获取java obejct和jsonproperty将生成whilc我将java对象转换为JSON。
VO课程:
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class FalconidePersonalizationVO {
private String recipient;
@JsonProperty("x-apiheader-cc")
private String xApiheaderCc;
@JsonProperty("x-apiheader")
private String xApiheader;
private List<FalconideAttachmentVO> attachments = null;
@JsonProperty("recipient_cc")
private List<String> recipientCc = null;
public String getRecipient() {
return recipient;
}
public void setRecipient(String recipient) {
this.recipient = recipient;
}
public String getXApiheaderCc() {
return xApiheaderCc;
}
public void setXApiheaderCc(String xApiheaderCc) {
this.xApiheaderCc = xApiheaderCc;
}
public String getXApiheader() {
return xApiheader;
}
public void setXApiheader(String xApiheader) {
this.xApiheader = xApiheader;
}
public List<FalconideAttachmentVO> getAttachments() {
return attachments;
}
public void setAttachments(List<FalconideAttachmentVO> attachments) {
this.attachments = attachments;
}
public List<String> getRecipientCc() {
return recipientCc;
}
public void setRecipientCc(List<String> recipientCc) {
this.recipientCc = recipientCc;
}
/* (non-Javadoc)
* @see java.lang.Object#toString()
*/
@Override
public String toString() {
return "PersonalizationVO [recipient=" + recipient + ", xApiheaderCc=" + xApiheaderCc + ", xApiheader="
+ xApiheader + ", attachments=" + attachments + ", recipientCc=" + recipientCc + "]";
}
}
主要课程:
public class FalconideAPICall {
public static void main(String[] args) throws JsonGenerationException, JsonMappingException, IOException {
try {
FalconideEmailVO falconideEmail = new FalconideEmailVO();
List<FalconidePersonalizationVO> personalizationList = new ArrayList<>();
FalconidePersonalizationVO personalization = new FalconidePersonalizationVO();
personalization.setRecipient("bharathi.senthilnathan@cognizant.com");
personalization.setXApiheader("ABC1234");
personalization.setXApiheaderCc("DEF1234");
List<String> recipientCc= new ArrayList<>();
recipientCc.add("XXXXX");
personalization.setRecipientCc(recipientCc);
personalizationList.add(personalization);
falconideEmail.setPersonalizations(personalizationList);
ObjectMapper mapperObj = new ObjectMapper();
String jsonStr = mapperObj.writerWithDefaultPrettyPrinter().writeValueAsString(falconideEmail);
System.out.println("jsonStr"+jsonStr);
} }
输出:
"personalizations" : [ {
"recipient" : "bharathi.senthilnathan@cognizant.com",
**"xapiheader" : "ABC1234", Which should not come
"xapiheaderCc" : "DEF1234", Which should not come **
**"x-apiheader-cc" : "DEF1234", xapiheader should come as x-apiheader-cc as I used @JSONProperty
"x-apiheader" : "ABC1234", xapiheader should come as x-apiheader-cc as I used @JSONProperty**
"recipient_cc" : [ "bharathi.senthilnathan@cognizant.com" ]
} ]
此处xapiheader和xapiheaderCc应转换为JSON。只有x-apiheader-cc和x-apiheader应该是JSON,因为我已经将@JSONProperty用于xapiheader和xapiheaderCc。
请你确认我弄错了。
答案 0 :(得分:0)
将@JsonAutoDetect(getterVisibility= JsonAutoDetect.Visibility.NONE)
添加到您的班级:
@JsonAutoDetect(getterVisibility= JsonAutoDetect.Visibility.NONE)
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class FalconidePersonalizationVO {
默认情况下,Jackson遵循java bean约定来输出json属性。因此,它会找到您的getX
方法和输出xapiheader
属性。
但您也可以使用@JsonProperty
为您的字段添加注释,以便另一个名为x-apiheader
的属性也会被取消。
禁用getterX
检测方法将阻止jackson输出getter字段。
答案 1 :(得分:0)
**************** 解决方案1 ****************
使用@JsonProperty注释getter / setter(现在注释字段不是必需的)
public class FalconidePersonalizationVO {
@JsonProperty("x-apiheader-cc")
private String xApiheaderCc;
@JsonProperty("x-apiheader")
private String xApiheader;
@JsonProperty("x-apiheader-cc")
public String getXApiheaderCc() {
return xApiheaderCc;
}
@JsonProperty("x-apiheader-cc")
public void setXApiheaderCc(String xApiheaderCc) {
this.xApiheaderCc = xApiheaderCc;
}
@JsonProperty("x-apiheader")
public String getXApiheader() {
return xApiheader;
}
@JsonProperty("x-apiheader")
public void setXApiheader(String xApiheader) {
this.xApiheader = xApiheader;
}
}
**************** 解决方案2 ****************
遵循setter / getter命名约定。在正常的命名约定中,字段名称的第一个字母大写,并以set / get开头。但是在这种情况下,因为第二个char是大写的,所以第一个char不是大写的。见https://stackoverflow.com/a/16146215/3295987
public class FalconidePersonalizationVO {
@JsonProperty("x-apiheader-cc")
private String xApiheaderCc;
@JsonProperty("x-apiheader")
private String xApiheader;
/*
* Setter / getter auto generated in eclipse
*/
// getXApiheaderCc -> getxApiheaderCc
public String getxApiheaderCc() {
return xApiheaderCc;
}
public void setxApiheaderCc(String xApiheaderCc) {
this.xApiheaderCc = xApiheaderCc;
}
public String getxApiheader() {
return xApiheader;
}
public void setxApiheader(String xApiheader) {
this.xApiheader = xApiheader;
}
}