我有 @JsonProperties 的 pojos。我使用这些来读取 JSON 并解析为 POJO。我现在必须发布这些格式为 XML 的 pojo。
成功发布所需的 XML 格式如下所示(注意命名空间类型、xsi 类型格式):
<network_objects>
<network_object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="subnetNetworkObjectDTO">
<name>TestSubnet</name>
<display_name>TestSubnet</display_name>
<global>false</global>
<application_id>3</application_id>
<type>subnet</type>
<ip>5.207.206.0</ip>
<netmask>255.255.254.0</netmask>
</network_object>
<network_object xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="networkObjectGroupDTO">
<name>Test01Subnets</name>
<display_name>Test01Subnets</display_name>
<application_id>3</application_id>
<type>group</type>
</network_object>
</network_objects>
我通过创建的应用程序类(如下所示)转换为 XML
ObjectMapper mapper = new XmlMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
byte[] val = mapper.writeValueAsBytes(myApp);
输出有点偏离,不包含 xmlns,xsi 看起来不同。它还具有“应用程序”作为根:
<Application>
<network_objects>
<network_object>
<network_object>
<@xsi.type>networkObjectGroupDTO</@xsi.type>
<name>name</name>
<display_name>displayName</display_name>
<application_id>3</application_id>
<type>group</type>
</network_object>
</network_object>
</network_objects>
</Application>
当我将类输出到 JSON 时,它看起来像预期的那样(没有“应用程序”作为根)。
ObjectMapper mapper = new ObjectMapper();
mapper.enable(SerializationFeature.INDENT_OUTPUT);
byte[] val = mapper.writeValueAsBytes(myApp);
{
"network_objects" : {
"network_object" : [ {
"@xsi.type" : "networkObjectGroupDTO",
"name" : "name",
"display_name" : "displayName",
"application_id" : 3,
"type" : "group"
}
}
}
为了正确格式化 XML,我需要使用 XmlMapper() 或 POJO 进行什么修改?
以下是用于此的 POJO。
应用类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"network_objects"
})
public class Application {
@JsonProperty("network_objects")
private NetworkObjects networkObjects;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("network_objects")
public NetworkObjects getNetworkObjects() {
return networkObjects;
}
@JsonProperty("network_objects")
public void setNetworkObjects(NetworkObjects networkObjects) {
this.networkObjects = networkObjects;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
网络对象类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"network_object"
})
public class NetworkObjects {
@JsonProperty("network_object")
private List<NetworkObject> networkObject = null;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("network_object")
public List<NetworkObject> getNetworkObject() {
return networkObject;
}
@JsonProperty("network_object")
public void setNetworkObject(List<NetworkObject> networkObject) {
this.networkObject = networkObject;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}
网络对象类:
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"@xsi.type",
"id",
"uid",
"name",
"display_name",
"global",
"comment",
"application_id",
"type",
"ip",
"access_allowed",
"member",
"last_ip",
"first_ip",
"netmask"
})
public class NetworkObject {
@JsonProperty("@xsi.type")
private String xsiType;
@JsonProperty("id")
private Integer id;
@JsonProperty("uid")
private String uid;
@JsonProperty("name")
private String name;
@JsonProperty("display_name")
private String displayName;
@JsonProperty("global")
private Boolean global;
@JsonProperty("comment")
private String comment;
@JsonProperty("application_id")
private Integer applicationId;
@JsonProperty("type")
private String type;
@JsonProperty("ip")
private String ip;
@JsonProperty("access_allowed")
private Boolean accessAllowed;
@JsonProperty("member")
private List<Member> member = null;
@JsonProperty("last_ip")
private String lastIp;
@JsonProperty("first_ip")
private String firstIp;
@JsonProperty("netmask")
private String netmask;
@JsonIgnore
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("@xsi.type")
public String getXsiType() {
return xsiType;
}
@JsonProperty("@xsi.type")
public void setXsiType(String xsiType) {
this.xsiType = xsiType;
}
@JsonProperty("id")
public Integer getId() {
return id;
}
@JsonProperty("id")
public void setId(Integer id) {
this.id = id;
}
@JsonProperty("uid")
public String getUid() {
return uid;
}
@JsonProperty("uid")
public void setUid(String uid) {
this.uid = uid;
}
@JsonProperty("name")
public String getName() {
return name;
}
@JsonProperty("name")
public void setName(String name) {
this.name = name;
}
@JsonProperty("display_name")
public String getDisplayName() {
return displayName;
}
@JsonProperty("display_name")
public void setDisplayName(String displayName) {
this.displayName = displayName;
}
@JsonProperty("global")
public Boolean getGlobal() {
return global;
}
@JsonProperty("global")
public void setGlobal(Boolean global) {
this.global = global;
}
@JsonProperty("comment")
public String getComment() {
return comment;
}
@JsonProperty("comment")
public void setComment(String comment) {
this.comment = comment;
}
@JsonProperty("application_id")
public Integer getApplicationId() {
return applicationId;
}
@JsonProperty("application_id")
public void setApplicationId(Integer applicationId) {
this.applicationId = applicationId;
}
@JsonProperty("type")
public String getType() {
return type;
}
@JsonProperty("type")
public void setType(String type) {
this.type = type;
}
@JsonProperty("ip")
public String getIp() {
return ip;
}
@JsonProperty("ip")
public void setIp(String ip) {
this.ip = ip;
}
@JsonProperty("access_allowed")
public Boolean getAccessAllowed() {
return accessAllowed;
}
@JsonProperty("access_allowed")
public void setAccessAllowed(Boolean accessAllowed) {
this.accessAllowed = accessAllowed;
}
@JsonProperty("member")
public List<Member> getMember() {
return member;
}
@JsonProperty("member")
public void setMember(List<Member> member) {
this.member = member;
}
@JsonProperty("last_ip")
public String getLastIp() {
return lastIp;
}
@JsonProperty("last_ip")
public void setLastIp(String lastIp) {
this.lastIp = lastIp;
}
@JsonProperty("first_ip")
public String getFirstIp() {
return firstIp;
}
@JsonProperty("first_ip")
public void setFirstIp(String firstIp) {
this.firstIp = firstIp;
}
@JsonProperty("netmask")
public String getNetmask() {
return netmask;
}
@JsonProperty("netmask")
public void setNetmask(String netmask) {
this.netmask = netmask;
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
}