package com.example.restservice;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import java.util.concurrent.atomic.AtomicLong;
@RestController
//Spring’s approach to building RESTful web services, HTTP requests are handled by a controller. These components are identified by the @RestController annotation, and the GreetingController
//This code uses Spring @RestController annotation, which marks the class as a controller where every method returns a domain object instead of a view. It is shorthand for including both @Controller and @ResponseBody.
public class GreetingController {
private static final;
String template = "Hello,%s!";
private final AtomicLong counter = new AtomicLong();
@GetMapping("/greeting") //get方法 GET requests for /greeting,
The @GetMapping annotation ensures that HTTP GET requests to /greeting are mapped to the greeting() method.
@GetMapping注释确保对greeting()方法的HTTP GET请求被映射到greeting()方法。
其他HTTP动词也有相应的注释(例如@PostMapping用于POST)。还有一个@RequestMapping注释,它们都是从这个注释派生出来的,可以作为同义词(例如@RequestMapping(method=GET))。
public Greeting greeting(@RequestParam(value = "name",defaultValue ="ShenXiaoDao") String name){
return new Greeting(counter.incrementAndGet(),String.format(template,name));
}
}