常用注解简介
一、常用
实体类:
ID表示自动增长:@TableId(type = IdType.AUTO)
对应数据库不存在的字段:@TableField(exist = false)
时间返回格式:@JsonFormat(timezone = "GMT+8", pattern = "yyyy-MM-dd HH:mm:ss")
时间入参格式:@DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss")
去数据的空值: @JsonInclude(JsonInclude.Include.NON_NULL)
控制层 dto入参可以为空:@RequestBody(required=false)
控制层返回数据格式:@PostMapping(value = "/userInfo", produces = "application/json;charset=UTF-8")
Json和XML @GetMapping(value = "/user-instance", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
@GetMapping(value = "/user-instance", produces = MediaType.APPLICATION_XML_VALUE)
二、注解 (annotations) 列表
@SpringBootApplication:
包含了 @ComponentScan、@Configuration 和 @EnableAutoConfiguration 注解。
其中 @ComponentScan 让 spring Boot 扫描到 Configuration 类并把它加入到程序上下文。
@Configuration 等同于 spring 的 XML 配置文件;使用 Java 代码可以检查类型安全。
**@EnableAutoConfiguration ** 自动配置。
**@ComponentScan ** 组件扫描,可自动发现和装配一些 Bean。
@Component 可配合 CommandLineRunner 使用,在程序启动后执行一些基础任务。
@RestController 注解是 @Controller 和 @ResponseBody 的合集, 表示这是个控制器 bean, 并且是将函数的返回值直 接填入 HTTP 响应体中, 是 REST 风格的控制器。
@Autowired 自动导入。(@Resources按名字,是JDK的,@Autowired按类型,是Spring的。)
@PathVariable 获取参数。
@JsonBackReference 解决嵌套外链问题。
@RepositoryRestResourcepublic 配合 spring-boot-starter-data-rest 使用。
三、全局异常处理
@ControllerAdvice:包含 @Component。可以被扫描到。统一处理异常(作为增强控制器BaseController)
浙公网安备 33010602011771号