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

GreetingControl.java 868B

1234567891011121314151617181920
  1. package hello;
  2. import java.util.concurrent.atomic.AtomicLong;
  3. import org.springframework.web.bind.annotation.RequestMapping;
  4. import org.springframework.web.bind.annotation.RequestParam;
  5. import org.springframework.web.bind.annotation.RestController;
  6. @RestController
  7. class GreetingController {
  8. private static final String template = "Hello, %s!";
  9. private final AtomicLong counter = new AtomicLong();
  10. @RequestMapping("/greeting")//BY Default get
  11. public Greeting greeting(@RequestParam(value="name", defaultValue="World") String name) {
  12. return new Greeting(counter.incrementAndGet(), String.format(template, name));
  13. // returns a new Greeting object with id and content attributes based on the next value from the counter
  14. //String.format(template, name);//and formats the given name by using the greeting template
  15. }
  16. }