这些注解先详细释义,后续再分类规范。
@RestController
@RestController 等同于 @Controller + @ResponseBody。
@Controller 表示这是个控制器类,是个被Spring容器管理的Bean;
@ResponseBody 表示方法的返回值是Http Response Body,实际是个JSON串,而不是跳转路径。
@GetMapping系列
@GetMapping 系列是Spring4.3中引入的替代 @RequestMapping 注解,用于解析请求路径,@RequestMapping(value = "/get", method = RequestMethod.GET)等同于@GetMapping("/get")。
该系列包括 @GetMapping、@PostMapping、@PutMapping、@DeleteMapping、@PatchMapping。
@GetMapping 是解析查询请求;
@PostMapping 是解析新增请求;
@PutMapping 是解析更新请求;
@DeleteMapping 是解析删除请求;
@PatchMapping 是解析部分更新请求;
@PatchMapping 跟 @PutMapping 区别是后者是全部替换,而 @PatchMapping 是部分更新。
@SpringBootApplication
@SpringBootApplication 等同于 @Configuration + @EnableAutoConfiguration + @ComponentScan。
@Configuration 表示被注解的类用于Bean定义;
@EnableAutoConfiguration 将所有符合自动配置条件的Bean加载到容器中;
@ComponentScan 扫描所在包中的Bean。
乱入 @JsonProperty 系列
@JsonProperty 系列不是Spring注解,但在Spring文档中用到,顺便补充。@JsonProperty 是jackson的系列注解,用于序列化。
@JsonProperty 作用在属性上,将该属性名称序列化为另一个名称;
@JsonIgnore 作用在属性(或方法上),使得json数据中不包含该属性;
@JsonIgnoreProperties 作用在类上,用于json序列化时忽略掉类一些属性;
@JsonSerialize 作用在属性或getter方法上,用在序列化时于解析嵌入的自定义代码;
@JsonDeserialize 作用在属性或setter方法上,用在反序列化时解析嵌入的自定义代码;
@JsonInclude 作用于属性上,用于指定属性值满足某条件则不参与序列化,如 @JsonInclude(JsonInclude.Include.NON_NULL) 表示该属性为null时不参与序列化。
@SpringBootTest
@SpringBootTest 用于测试SpringBoot应用,它会寻找同名包下的有 @SpringBootApplication 注解的类,启动容器。
如果同名包下找不到或有多个 @SpringBootApplication 类,都会报错。注意,@SpringBootApplication 类必须直接在同名包下,不能是在子包下。