I have pojos with @JsonProperties. I use these to read JSON and parse to POJO. I am now having to post these pojos formatted as XML.

The Required XML format to successfully post looks like this (note the namespace type, xsi type formatting):

TestSubnet

TestSubnet

false

3

subnet

5.207.206.0

255.255.254.0

Test01Subnets

Test01Subnets

3

group

I pass the created Application Class (shown below) to convert to XML

ObjectMapper mapper = new XmlMapper();

mapper.enable(SerializationFeature.INDENT_OUTPUT);

byte[] val = mapper.writeValueAsBytes(myApp);

The output is a bit off and does not contain the xmlns and the xsi looks different. It also has 'Application' as root:

networkObjectGroupDTO@xsi.type>

name

displayName

3

group

When I output the class to JSON, it looks as expected (No "Application" as root).

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"

}

}

}

What do I need to modify with my XmlMapper() or POJOs in order to get the XML formatted correctly?

Below are the POJOs used for this.

Application Class:

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({

"network_objects"

})

public class Application {

@JsonProperty("network_objects")

private NetworkObjects networkObjects;

@JsonIgnore

private Map additionalProperties = new HashMap();

@JsonProperty("network_objects")

public NetworkObjects getNetworkObjects() {

return networkObjects;

}

@JsonProperty("network_objects")

public void setNetworkObjects(NetworkObjects networkObjects) {

this.networkObjects = networkObjects;

}

@JsonAnyGetter

public Map getAdditionalProperties() {

return this.additionalProperties;

}

@JsonAnySetter

public void setAdditionalProperty(String name, Object value) {

this.additionalProperties.put(name, value);

}

}

NetworkObjects Class:

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({

"network_object"

})

public class NetworkObjects {

@JsonProperty("network_object")

private List networkObject = null;

@JsonIgnore

private Map additionalProperties = new HashMap();

@JsonProperty("network_object")

public List getNetworkObject() {

return networkObject;

}

@JsonProperty("network_object")

public void setNetworkObject(List networkObject) {

this.networkObject = networkObject;

}

@JsonAnyGetter

public Map getAdditionalProperties() {

return this.additionalProperties;

}

@JsonAnySetter

public void setAdditionalProperty(String name, Object value) {

this.additionalProperties.put(name, value);

}

}

NetworkObject Class:

@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 = null;

@JsonProperty("last_ip")

private String lastIp;

@JsonProperty("first_ip")

private String firstIp;

@JsonProperty("netmask")

private String netmask;

@JsonIgnore

private Map additionalProperties = new HashMap();

@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 getMember() {

return member;

}

@JsonProperty("member")

public void setMember(List 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 getAdditionalProperties() {

return this.additionalProperties;

}

@JsonAnySetter

public void setAdditionalProperty(String name, Object value) {

this.additionalProperties.put(name, value);

}

}

解决方案

I used your bean classes and json string. Almost everything is possible with the conversion, but there are some issues. I got this so far, if someone can derive the final solution it will be great.

Application.java

Not really any changes.

NetworkObject.java

@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("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("@xsi.type")

// CHANGE: You cannot have same element name and attribute name, so I had to change this to xtype if someone knows how to tackle this, that will be final answer

@JacksonXmlProperty(localName = "xtype", isAttribute = true, namespace = "http://www.w3.org/2001/XMLSchema-instance")

private String xsiType;

@JsonProperty("ip")

private String ip;

@JsonProperty("access_allowed")

private Boolean accessAllowed;

@JsonProperty("member")

private List member = null;

@JsonProperty("last_ip")

private String lastIp;

@JsonProperty("first_ip")

private String firstIp;

@JsonProperty("netmask")

private String netmask;

@JsonIgnore

private Map additionalProperties = new HashMap();

// CHANGE: We don't really need on getter setters @JsonProperty("@xsi.type")

public String getXsiType() {

return xsiType;

}

// CHANGE: We don't really need on getter setters @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 getMember() {

return member;

}

@JsonProperty("member")

public void setMember(List 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 getAdditionalProperties() {

return this.additionalProperties;

}

@JsonAnySetter

public void setAdditionalProperty(String name, Object value) {

this.additionalProperties.put(name, value);

}

}

NetworkObjects

@JsonInclude(JsonInclude.Include.NON_NULL)

@JsonPropertyOrder({"network_object"})

// CHANGE: To provide root element name

@JacksonXmlRootElement(localName = "network_objects")

public class NetworkObjects {

@JsonProperty("network_object")

@JacksonXmlElementWrapper(useWrapping = false)

// CHANGE: To ignore

private List networkObject = null;

@JsonIgnore

private Map additionalProperties = new HashMap();

@JsonProperty("network_object")

public List getNetworkObject() {

return networkObject;

}

@JsonProperty("network_object")

public void setNetworkObject(List networkObject) {

this.networkObject = networkObject;

}

@JsonAnyGetter

public Map getAdditionalProperties() {

return this.additionalProperties;

}

@JsonAnySetter

public void setAdditionalProperty(String name, Object value) {

this.additionalProperties.put(name, value);

}

}

Main.java

public static void main(String[] args) throws JsonParseException, JsonMappingException, IOException {

ObjectMapper jsonOM = new ObjectMapper();

String jsomn = Files.readAllLines(Paths.get("sample.json"), StandardCharsets.US_ASCII).stream().collect(Collectors.joining(""));

Application myApp = jsonOM.readValue(jsomn, Application.class);

ObjectMapper mapper = new XmlMapper();

mapper.enable(SerializationFeature.INDENT_OUTPUT);

// Not writing entire object

System.out.println(mapper.writer().writeValueAsString(myApp.getNetworkObjects()));

}

OUPUT

name

displayName

3

group

Logo

华为开发者空间,是为全球开发者打造的专属开发空间,汇聚了华为优质开发资源及工具,致力于让每一位开发者拥有一台云主机,基于华为根生态开发、创新。

更多推荐