我有一个控制器:
@RequestMapping(value="/getContact", method=RequestMethod.GET,produces=MediaType.APPLICATION_JSON_VALUE)
public Contact getContact () {
Contact s1 = new Contact();
s1.setContactName("Avijit");
s1.setContactType("Primary");
return s1;
}
联系方式:
public class Contact {
private String contactName;
private String contactType;
public String getContactName() {
return contactName;
}
public void setContactName(String contactNm) {
this.contactName = contactNm;
}
public String getContactType() {
return contactType;
}
public void setContactType(String contactType) {
this.contactType = contactType;
}
}
在这种情况下,从Controller返回的JSON对象是:
{
"contactName": "Avijit",
"contactType": "Primary"
}
现在,如果我在模型类中添加@JsonProperty:
public class Contact {
@JsonProperty(value="contactName")
private String contactName;
private String contactType;
public String getContactName() {
return contactName;
}
public void setContactName(String contactNm) {
this.contactName = contactNm;
}
public String getContactType() {
return contactType;
}
public void setContactType(String contactType) {
this.contactType = contactType;
}
}
当我在模型类中使用@JsonProperty时,从Controller返回的JSON对象:
{
"contactType": "Primary",
"contactName": "Avijit"
}
我的问题是-即使这两个标签都具有相同的名称,为什么还会发生这种反转?
注意:我已经阅读了此链接,并且我非常了解@JsonPropertyOrder Order of JSON objects using Jackson's ObjectMapper