被注解标识后,controller返回序列化参数后变为this_name,当接受application/json编码格式的参数时,
同样需要接收参数为this_name的参数.但是当用表单提交时,则必须传thisName或ThisName才能接收
这里需要注意的是将对象转换成json字符串使用的方法是fasterxml.jackson提供的!!
@JsonProperty不仅仅是在序列化的时候有用,反序列化的时候也有用,比如有些接口返回的是json字符串,命名又不是标准的驼峰形式,在映射成对象的时候,将类的属性上加上@JsonProperty注解,里面写上返回的json串对应的名字
jackson的maven依赖
[html] view plain copy
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.5.3</version>
</dependency>
@JsonProperty 此注解用于属性上,作用是把该属性的名称序列化为另外一个名称,如把trueName属性序列化为name,@JsonProperty("name")。
[java] view plain copy
import com.fasterxml.jackson.annotation.JsonProperty;
public class Student {
@JsonProperty("name")
private String trueName;
public String getTrueName() {
return trueName;
}
public void setTrueName(String trueName) {
this.trueName = trueName;
}
}
测试一下
[java] view plain copy
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
public class Main {
public static void main(String[] args) throws JsonProcessingException {
Student student = new Student();
student.setTrueName("张三");
System.out.println(new ObjectMapper().writeValueAsString(student));
}
}
得到结果
[html] view plain copy
{"name":"张三"}
@JsonProperty("this_name")
private String thisName;