我需要为给定的JSON文件生成POJO,然后将JSON值注入POJO。我必须按照JSON文件中指定的顺序将其转换为XML。 示例:
{
"Body": {
"Request": {
"appId": "Admin",
"deviceId": "8DFAF95202C247A98315F2EE037B3375",
"clientNonce": "116",
"requestKey": "",
"async": "false",
"source": "APPZILLON",
"screenId": "LandingPage",
"userId": "",
"requestId": "CSNONCE",
"os": "WEB",
"sessionId": "",
"origination": "error",
"status": "true",
"node1":{
"element1":"element1 value",
"node2":{
"element2":"element2 value"
}
}
}
}
}
我已经给出了上述JSON,我已经使用了 jsonSchema2POJO 并生成了POJO,如下所示:
package com.convert;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.validation.Valid;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Body"
})
public class Input {
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("Body")
@Valid
private Body body;
@JsonIgnore
@Valid
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("Body")
public Body getBody() {
return body;
}
@JsonProperty("Body")
public void setBody(Body body) {
this.body = body;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(body).append(additionalProperties).toHashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Input) == false) {
return false;
}
Input rhs = ((Input) other);
return new EqualsBuilder().append(body, rhs.body).append(additionalProperties, rhs.additionalProperties).isEquals();
}
}
和以下子类:
package com.convert;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.validation.Valid;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"Request"
})
public class Body {
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("Request")
@Valid
private Request request;
@JsonIgnore
@Valid
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("Request")
public Request getRequest() {
return request;
}
@JsonProperty("Request")
public void setRequest(Request request) {
this.request = request;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(request).append(additionalProperties).toHashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Body) == false) {
return false;
}
Body rhs = ((Body) other);
return new EqualsBuilder().append(request, rhs.request).append(additionalProperties, rhs.additionalProperties).isEquals();
}
}
像这样的孩子,带有所有JSON相关注释
package com.convert;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nullable;
import javax.validation.Valid;
import com.fasterxml.jackson.annotation.JsonAnyGetter;
import com.fasterxml.jackson.annotation.JsonAnySetter;
import com.fasterxml.jackson.annotation.JsonIgnore;
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.annotation.JsonProperty;
import com.fasterxml.jackson.annotation.JsonPropertyOrder;
import org.apache.commons.lang.builder.EqualsBuilder;
import org.apache.commons.lang.builder.HashCodeBuilder;
import org.apache.commons.lang.builder.ToStringBuilder;
@JsonInclude(JsonInclude.Include.NON_NULL)
@JsonPropertyOrder({
"appId",
"deviceId",
"clientNonce",
"requestKey",
"async",
"source",
"screenId",
"userId",
"requestId",
"os",
"sessionId",
"origination",
"status",
"node1"
})
public class Request {
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("appId")
private String appId;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("deviceId")
private String deviceId;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("clientNonce")
private String clientNonce;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("requestKey")
private String requestKey;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("async")
private String async;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("source")
private String source;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("screenId")
private String screenId;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("userId")
private String userId;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("requestId")
private String requestId;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("os")
private String os;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("sessionId")
private String sessionId;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("origination")
private String origination;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("status")
private String status;
/**
*
* (Can be null)
*
*/
@Nullable
@JsonProperty("node1")
@Valid
private Node1 node1;
@JsonIgnore
@Valid
private Map<String, Object> additionalProperties = new HashMap<String, Object>();
@JsonProperty("appId")
public String getAppId() {
return appId;
}
@JsonProperty("appId")
public void setAppId(String appId) {
this.appId = appId;
}
@JsonProperty("deviceId")
public String getDeviceId() {
return deviceId;
}
@JsonProperty("deviceId")
public void setDeviceId(String deviceId) {
this.deviceId = deviceId;
}
@JsonProperty("clientNonce")
public String getClientNonce() {
return clientNonce;
}
@JsonProperty("clientNonce")
public void setClientNonce(String clientNonce) {
this.clientNonce = clientNonce;
}
@JsonProperty("requestKey")
public String getRequestKey() {
return requestKey;
}
@JsonProperty("requestKey")
public void setRequestKey(String requestKey) {
this.requestKey = requestKey;
}
@JsonProperty("async")
public String getAsync() {
return async;
}
@JsonProperty("async")
public void setAsync(String async) {
this.async = async;
}
@JsonProperty("source")
public String getSource() {
return source;
}
@JsonProperty("source")
public void setSource(String source) {
this.source = source;
}
@JsonProperty("screenId")
public String getScreenId() {
return screenId;
}
@JsonProperty("screenId")
public void setScreenId(String screenId) {
this.screenId = screenId;
}
@JsonProperty("userId")
public String getUserId() {
return userId;
}
@JsonProperty("userId")
public void setUserId(String userId) {
this.userId = userId;
}
@JsonProperty("requestId")
public String getRequestId() {
return requestId;
}
@JsonProperty("requestId")
public void setRequestId(String requestId) {
this.requestId = requestId;
}
@JsonProperty("os")
public String getOs() {
return os;
}
@JsonProperty("os")
public void setOs(String os) {
this.os = os;
}
@JsonProperty("sessionId")
public String getSessionId() {
return sessionId;
}
@JsonProperty("sessionId")
public void setSessionId(String sessionId) {
this.sessionId = sessionId;
}
@JsonProperty("origination")
public String getOrigination() {
return origination;
}
@JsonProperty("origination")
public void setOrigination(String origination) {
this.origination = origination;
}
@JsonProperty("status")
public String getStatus() {
return status;
}
@JsonProperty("status")
public void setStatus(String status) {
this.status = status;
}
@JsonProperty("node1")
public Node1 getNode1() {
return node1;
}
@JsonProperty("node1")
public void setNode1(Node1 node1) {
this.node1 = node1;
}
@Override
public String toString() {
return ToStringBuilder.reflectionToString(this);
}
@JsonAnyGetter
public Map<String, Object> getAdditionalProperties() {
return this.additionalProperties;
}
@JsonAnySetter
public void setAdditionalProperty(String name, Object value) {
this.additionalProperties.put(name, value);
}
@Override
public int hashCode() {
return new HashCodeBuilder().append(appId).append(deviceId).append(clientNonce).append(requestKey).append(async).append(source).append(screenId).append(userId).append(requestId).append(os).append(sessionId).append(origination).append(status).append(node1).append(additionalProperties).toHashCode();
}
@Override
public boolean equals(Object other) {
if (other == this) {
return true;
}
if ((other instanceof Request) == false) {
return false;
}
Request rhs = ((Request) other);
return new EqualsBuilder().append(appId, rhs.appId).append(deviceId, rhs.deviceId).append(clientNonce, rhs.clientNonce).append(requestKey, rhs.requestKey).append(async, rhs.async).append(source, rhs.source).append(screenId, rhs.screenId).append(userId, rhs.userId).append(requestId, rhs.requestId).append(os, rhs.os).append(sessionId, rhs.sessionId).append(origination, rhs.origination).append(status, rhs.status).append(node1, rhs.node1).append(additionalProperties, rhs.additionalProperties).isEquals();
}
}
当我从上述Java Bean生成Xml时,它没有顺序
@JsonPropertyOrder({
"appId",
"deviceId",
"clientNonce",
"requestKey",
"async",
"source",
"screenId",
"userId",
"requestId",
"os",
"sessionId",
"origination",
"status",
"node1"
})
如果我将 @JsonPropertyOrder(= 替换为 @XmlType(propOrder = ),那么它将按顺序出现。请告诉我,如何实现此目标,还建议还有其他方法可以做。