-
代码结构
-
pom.xml 依赖
-
Domain Class - Greeting
public class Greeting {private final long id;
private final String content;public Greeting(long id, String content) {
this.id = id;
this.content = content;
}public long getId() {
return id;
}public String getContent() {
return content;
}
} -
Controller Class - GreetingControllor
@RestController
public class GreetingController {private Logger logger = LoggerFactory.getLogger(GreetingController.class);
private static final String template = “Hello, %s!”;
private final AtomicLong counter = new AtomicLong();@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value = “name”, defaultValue = “World”) String name) {
logger.info("Obtained parameter as {} ", name);
long id = counter.incrementAndGet();
String content = String.format(template, name);
logger.info(“Generated Result: {} - {}”, id, content);
return new Greeting(id, content);
}
}
- 启动项目, 测试
6.1 默认显示 World