Building a RESTful Web Service :: Learn how to create a RESTful web service with Spring. :: spring-boot http://spring.io/guides/gs/rest-service/

GreetingController.java 753B

123456789101112131415161718192021
  1. package hello;
  2. import java.util.concurrent.atomic.AtomicLong;
  3. import org.springframework.stereotype.Controller;
  4. import org.springframework.web.bind.annotation.RequestMapping;
  5. import org.springframework.web.bind.annotation.RequestParam;
  6. import org.springframework.web.bind.annotation.ResponseBody;
  7. @Controller
  8. public class GreetingController {
  9. private static final String template = "Hello, %s!";
  10. private final AtomicLong counter = new AtomicLong();
  11. @RequestMapping("/greeting")
  12. public @ResponseBody Greeting greeting(
  13. @RequestParam(value="name", required=false, defaultValue="World") String name) {
  14. return new Greeting(counter.incrementAndGet(),
  15. String.format(template, name));
  16. }
  17. }