大家好,又见面了,我是你们的朋友全栈君。
参考博客:http://www.cnblogs.com/yucy/p/9057049.html
1.经常遇到这种情况,传过来json的key名和我们javaBean的属性名不一样,导致接收不到:
Child类用来接收传过来的json串:
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串:
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:
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接收:
可以看到此时是接受不到的。
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);
}
}
再次接受就能收到了:
5)使用@JSONField(serialize = false) 在get方法上添加来过滤掉不需要序列化的字段:
Child类:
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字段没有被序列化:
—–
发布者:全栈程序员-用户IM,转载请注明出处:https://javaforall.cn/125783.html原文链接:https://javaforall.cn
【正版授权,激活自己账号】: Jetbrains全家桶Ide使用,1年售后保障,每天仅需1毛
【官方授权 正版激活】: 官方授权 正版激活 支持Jetbrains家族下所有IDE 使用个人JB账号...