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

HelloWorldResource.java 801B

123456789101112131415161718192021222324
  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.RequestMethod;
  6. import org.springframework.web.bind.annotation.RequestParam;
  7. import org.springframework.web.bind.annotation.ResponseBody;
  8. @Controller
  9. @RequestMapping("/hello-world")
  10. public class HelloWorldResource {
  11. private static final String template = "Hello, %s!";
  12. private final AtomicLong counter = new AtomicLong();
  13. @RequestMapping(method=RequestMethod.GET)
  14. public @ResponseBody Saying sayHello(@RequestParam(value="name", required=false, defaultValue="Stranger") String name) {
  15. return new Saying(counter.incrementAndGet(), String.format(template, name));
  16. }
  17. }