Jackson返回结果处理的4种注解
需要jar包
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.9.9</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-annotations</artifactId>
<version>2.9.9</version>
</dependency>
如果项目是基于springboot创建,只需要引入
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
具体效果:亲测有效
@JsonIgnore //忽略id不显示
private Integer id;
@JsonInclude(JsonInclude.Include.NON_NULL) //如果当前值为空不显示
private String name;
@JsonProperty("abc") // 相当于给phone 字段起别名
private String phone;
@JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss",locale = "zh",timezone = "GMT+8")//格式化日期
private Date createTime;