Spring 、Spring Boot常用注解
@Autowired @Qualifier("chinese") private Man man;
@Configuration public class Conf { @Bean public Car car() { Car car = new Car(); car.setWheel(wheel()); return car; } @Bean public Wheel wheel() { return new Wheel(); } }
10、@ResponseBody 用该注解修饰的函数,会将结果直接填充到HTTP的响应体中,一般用于构建RESTful web services的api;
11、@RestController = @ResponseBody+@Controller;
12、@RequestMapping 注解会将 HTTP 请求映射到 MVC 和 REST 控制器的处理方法上。
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping("/get") String get() { //mapped to hostname:port/home/ return "Hello from get"; } @RequestMapping("/index") String index() { //mapped to hostname:port/home/index/ return "Hello from index"; } }
多个请求映射到一个方法上去,如下:
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = { "", "/page", "page*", "view/*,**/msg" }) String indexMultipleMapping() { return "Hello from index multiple mapping."; } }
@RequestMapping 注解能够处理 HTTP 请求的方法, 比如 GET, PUT, POST, DELETE 以及 PATCH。指定请求映射到一个特定的 HTTP 方法,需要在 @RequestMapping 中使用 method 来声明 HTTP 请求所使用的方法类型,如下:
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(method = RequestMethod.GET) String get() { return "Hello from get"; } @RequestMapping(method = RequestMethod.DELETE) String delete() { return "Hello from delete"; } @RequestMapping(method = RequestMethod.POST) String post() { return "Hello from post"; } @RequestMapping(method = RequestMethod.PUT) String put() { return "Hello from put"; } @RequestMapping(method = RequestMethod.PATCH) String patch() { return "Hello from patch"; } }
备注:该注解详细介绍,参考:http://www.cnblogs.com/DreamScience/p/8947961.html
13、@RequestParam 注解指定了需要被映射到处理方法参数的请求参数;配合 @RequestMapping 一起使用,可以将请求的参数同处理方法的参数绑定在一起。
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/id") String getIdByValue(@RequestParam("id") String personId) { System.out.println("ID is " + personId); return "Get ID from query string of URL with value element"; } @RequestMapping(value = "/personId") String getId(@RequestParam String personId) { System.out.println("ID is " + personId); return "Get ID from query string of URL without value element"; } }
@RequestParam 注解的 required 这个参数定义了参数值是否是必须要传的
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/name") String getName(@RequestParam(value = "person", required = false) String personName) { return "Required element of request param"; } }
@RequestParam 的 defaultValue 取值就是用来给取值为空的请求参数提供一个默认值的。
@RestController @RequestMapping("/home") public class IndexController { @RequestMapping(value = "/name") String getName(@RequestParam(value = "person", defaultValue = "John") String personName) { return "Required element of request param"; } }
14、@EnableAutoConfiguration 自动配置,这个注解告诉Spring Boot根据添加的jar依赖猜测你想如何配置Spring。auto-configuration将假定你正在开发一个web应用并相应地对Spring进行设置。
15、@ComponentScan 自动扫描@Component, @Controller, @Service, and @Repository注解组件类装入Spring的bean容器。