本文最后更新于:March 26, 2023 am
SpringBoot框架中有两个非常重要的策略:开箱即用和约定优于配置。其设计目的是用来简化新Spring应用的初始搭建以及开发过程。该框架使用了特定的方式来进行配置,从而使开发人员不再需要定义样板化的配置。
目录
假如有这么一个需要返回给前端展示的类:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.tothefor.antdemo.json;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Builder; import lombok.Data;
import java.util.Date;
@Data @Builder public class Person { private String username; private String website; private Integer age; private String password; private Date createTime; private Date updateTime; }
|
但是,我不想把age和password进行返回,需要怎么处理呢?有两种方式:@JsonIgnoreProperties、@JsonIgnore。具体演示见下。
过滤数据
将指定数据过滤出来不返回。可以使用两个Json注解进行过滤:@JsonIgnoreProperties、@JsonIgnore。
JsonIgnoreProperties
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
| package com.tothefor.antdemo.json;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Builder; import lombok.Data;
import java.util.Date;
@JsonIgnoreProperties({"age","password"}) @Data @Builder public class Person { private String username; private String website; private Integer age; private String password; private Date createTime; private Date updateTime; }
|
JsonIgnore
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27
| package com.tothefor.antdemo.json;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Builder; import lombok.Data;
import java.util.Date;
@Data @Builder public class Person { private String username; private String website; @JsonIgnore private Integer age; @JsonIgnore private String password; private Date createTime; private Date updateTime; }
|
注意
当以上两者注解同时出现时,JsonIgnoreProperties生效,如下所示:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28
| package com.tothefor.antdemo.json;
import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Builder; import lombok.Data;
import java.util.Date;
@JsonIgnoreProperties({"age","password"}) @Data @Builder public class Person { private String username; private String website; @JsonIgnore(value = false) private Integer age; @JsonIgnore(value = false) private String password; private Date createTime; private Date updateTime; }
|
运行程序后,查看页面返回的数据仍然没有age和password字段的数据。
格式化
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
| package com.tothefor.antdemo.json;
import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.annotation.JsonIgnore; import com.fasterxml.jackson.annotation.JsonIgnoreProperties; import lombok.Builder; import lombok.Data;
import java.util.Date;
@Data @Builder public class Person { private String username; private String website; private Integer age; private String password; @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8") private Date createTime; @JsonFormat(shape=JsonFormat.Shape.STRING, pattern="yyyy-MM-dd HH:mm:ss", timezone="GMT+8") private Date updateTime; }
|
扁平化对象
先有三个类如下:
| public class Show { private Man man; private Person person; }
public class Person { private String username; private String website; }
public class Man { private String name; private String web; }
|
正常返回Show类的数据,那么返回的格式是这样的:
| { "man": { "name": "man-name", "web": "man-website" }, "person": { "username": "person-name", "website": "person-website" } }
|
但是,通过@JsonUnwrapped处理后,就可以实现扁平化,使用如下:
| public class Show { @JsonUnwrapped private Man man; @JsonUnwrapped private Person person; }
|
那么再返回的数据格式是这样:
| { "name": "man-name", "web": "man-website", "username": "person-name", "website": "person-website" }
|
注意
可以看见,上面Man和Person类中的属性我并没有取一样的,如果取一样的,那么返回的就只有最下面的对象属性,如下返回数据:
| { "username": "person-name", "website": "person-website", }
|
所以,如果需要使用扁平化,就需要保证属性名称不同。但是,正常开发中肯定多多少少都会有重复的命名,那这时我们需要怎么处理呢?@JsonUnwrapped自带了两个属性:prefix、suffix。他们两个可以用来给被@JsonUnwrapped修饰的类的属性名称前或后拼接指定的字符串。如下所示:
| public class Show { @JsonUnwrapped(prefix = "man-pre",suffix = "man-suf") private Man man; @JsonUnwrapped private Person person; }
|
那么,再次返回的数据格式就如下所示:
| { "man-preusernameman-suf": "man-name", "man-prewebsiteman-suf": "man-website", "username": "person-name", "website": "person-website" }
|