java jsonfield_@JSONField使用

java jsonfield_@JSONField使用参考博客:http://www.cnblogs.com/yucy/p/9057049.html1.经常遇到这种情况,传过来json的key名和我们javaBean的属性名不一样,导致接收不到:Child类用来接收传过来的json串:packagecom.test.dto;importorg.apache.commons.lang3.builder.ToStringBuilder;importorg…

参考博客:http://www.cnblogs.com/yucy/p/9057049.html

1.经常遇到这种情况,传过来json的key名和我们javaBean的属性名不一样,导致接收不到:

Child类用来接收传过来的json串:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.test.dto;importorg.apache.commons.lang3.builder.ToStringBuilder;importorg.apache.commons.lang3.builder.ToStringStyle;public classChild {privateString childName;privateString childAge;privateString chileFather;publicString getChildName() {returnchildName;

}public voidsetChildName(String childName) {this.childName =childName;

}publicString getChildAge() {returnchildAge;

}public voidsetChildAge(String childAge) {this.childAge =childAge;

}publicString getChileFather() {returnchileFather;

}public voidsetChileFather(String chileFather) {this.chileFather =chileFather;

}

@OverridepublicString toString() {return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);

}

}

View Code

Controller来接收json串:

发送的json串:

8e768dc8eb89828b87945e674c63d089.png

packagecom.test.controller;importcom.test.dto.Child;importorg.springframework.stereotype.Controller;importorg.springframework.web.bind.annotation.RequestBody;importorg.springframework.web.bind.annotation.RequestMapping;

@Controllerpublic classTestController2 {/*** jsonField测试

*@paramchild*/@RequestMapping(“/testJsonField.do”)public voidtestJsonField(@RequestBody Child child){

System.out.println(child);

}

}

打印出来,看到是收不到的:

Child[childName=,childAge=,chileFather=]

2.对于这种@RequestBody的使用@JsonProperty将传过来的json key名能够映射到javaBean上:

Child:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.test.dto;importcom.fasterxml.jackson.annotation.JsonProperty;importorg.apache.commons.lang3.builder.ToStringBuilder;importorg.apache.commons.lang3.builder.ToStringStyle;public classChild {privateString childName;privateString childAge;privateString chileFather;publicString getChildName() {returnchildName;

}

@JsonProperty(value= “child_name”)public voidsetChildName(String childName) {this.childName =childName;

}publicString getChildAge() {returnchildAge;

}

@JsonProperty(value= “child_age”)public voidsetChildAge(String childAge) {this.childAge =childAge;

}publicString getChileFather() {returnchileFather;

}public voidsetChileFather(String chileFather) {this.chileFather =chileFather;

}

@OverridepublicString toString() {return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);

}

}

View Code

这样能收到了:

Child[childName=小孩儿,childAge=4,chileFather=]

3.当使用fastjson解析数据时。

1)Child类此时不加任何注解。

2)发送请求:http://localhost:8081/testJsonField.do?childStr={child_name:123, child_age:1}

3)controller接收:

faa4883cba365fdb170260a6bbd906ce.png

可以看到此时是接受不到的。

4)我们在Child类属性的set方法上加上@JSONField注解,name来匹配json串中的key:

public classChild {privateString childName;privateString childAge;privateString chileFather;publicString getChildName() {returnchildName;

}

@JSONField(name=”child_name”)public voidsetChildName(String childName) {this.childName =childName;

}publicString getChildAge() {returnchildAge;

}

@JSONField(name=”child_age”)public voidsetChildAge(String childAge) {this.childAge =childAge;

}publicString getChileFather() {returnchileFather;

}public voidsetChileFather(String chileFather) {this.chileFather =chileFather;

}

@OverridepublicString toString() {return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);

}

}

再次接受就能收到了:

98e379c990250ca2c9d474b2084a845a.png

5)使用@JSONField(serialize = false)   在get方法上添加来过滤掉不需要序列化的字段:

Child类:

8f900a89c6347c561fdf2122f13be562.png

961ddebeb323a10fe0623af514929fc1.png

packagecom.test.dto;importcom.alibaba.fastjson.annotation.JSONField;importorg.apache.commons.lang3.builder.ToStringBuilder;importorg.apache.commons.lang3.builder.ToStringStyle;public classChild {privateString childName;privateString childAge;privateString chileFather;publicString getChildName() {returnchildName;

}

@JSONField(name=”child_name”)public voidsetChildName(String childName) {this.childName =childName;

}publicString getChildAge() {returnchildAge;

}

@JSONField(name=”child_age”)public voidsetChildAge(String childAge) {this.childAge =childAge;

}

@JSONField(serialize= false)publicString getChileFather() {returnchileFather;

}public voidsetChileFather(String chileFather) {this.chileFather =chileFather;

}

@OverridepublicString toString() {return ToStringBuilder.reflectionToString(this, ToStringStyle.SHORT_PREFIX_STYLE);

}

}

View Code

我们可以看到childFather字段没有被序列化:

59a02d307c5d2f77d9a6460ee8e0fb1f.png

—–

今天的文章java jsonfield_@JSONField使用分享到此就结束了,感谢您的阅读。

版权声明:本文内容由互联网用户自发贡献,该文观点仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 举报,一经查实,本站将立刻删除。
如需转载请保留出处:http://bianchenghao.cn/5889.html

(0)
编程小号编程小号

相关推荐

发表回复

您的电子邮箱地址不会被公开。 必填项已用 * 标注