1.响应json时排除对象中,属性值为Null,的属性。
在Bean类上加@JsonInclude(JsonInclude.Include.NON_NULL)
复制代码
1
2
3
4
5
6
7
8
9
10
11@JsonInclude(JsonInclude.Include.NON_NULL) public class Video { private Integer id; //id private String title; //标题 private String intro; //简介 ........ }
2.1 响应json时,指定属性名,响应。
在指定属性的 get方法上面使用@JsonProperty(" ") 注解
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Video { private Integer id; //id private String title; //标题 @JsonProperty("video_name") public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } ........ }
2.2 在Json字符串作为请求参数时,要绑定到Bean对象属性上。
如果json中的字符串名和Bean对象中的属性名不一致可能就会导致绑定不上。
在Bean指定属性的 set方法上面使用@JsonProperty(" ") 注解
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Video { private Integer id; //id private String title; //标题 public String getTitle() { return title; } @JsonProperty("video_name") public void setTitle(String title) { this.title = title; } ........ }
2.直接在 Bean中指定属性上面使用@JsonProperty(" ") 注解,此时,不管是响应还是注入都可以使用指定名称。绑定或者响应
复制代码
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15public class Video { private Integer id; //id @JsonProperty("video_name") private String title; //标题 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } ........ }
3.响应json时忽略某些属性 使用 @JsonIgnore注解
· 如果直接在title属性上使用,响应时Json字符串中就不会再有这个title属性。
· 但是:当在新增Video的时候Json字符串参数绑定VideoBean时,title属性也被忽略。导致绑定不上这个值。
· 解决办法就是,不在title属性上直接加这个注解,在title对应的get方法上面使用这个注解
复制代码
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
27public class Video { private Integer id; //id @JsonIgnore private String title; //标题 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; } /**************************************************************************/ public class Video { private Integer id; //id private String title; //标题 @JsonIgnore //表示序列化为json时忽略 public String getTitle() { return title; } public void setTitle(String title) { this.title = title; }
3.响应json时指定日期格式
在日期属性上面加入注解
@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = “yyyy-MM-dd”, timezone = “GMT+8”)
复制代码
1
2
3
4
5
6
7
8
9
10
11public class Video { private Integer id; //id private String title; //标题 @JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd", timezone = "GMT+8") private Date createdAt; //创建日期 ........ }
最后
以上就是鳗鱼书包最近收集整理的关于对象转Json字符串时、使用JackSon来操作属性的全部内容,更多相关对象转Json字符串时、使用JackSon来操作属性内容请搜索靠谱客的其他文章。
本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
发表评论 取消回复