使用JaxbAnnotationModule将复杂的Json字符串转换为Java对象

时间:2018-08-24 06:46:26

标签: java json spring jackson annotations

我正在尝试转换包含子节点的json字符串
具有复杂结构和各种Java对象的对象。

运行时
- name: Download calibre-web dependencies into vendor subdirectory. command: pip install --target vendor -r ./requirements.txt args: chdir: /install/directory 收到此错误

我该如何解决这个问题?

JsonString

Root name 'm2m:rsp' does not match expected ('rsp') for type [simple type, class com.dkitec.simulator.onem2m.domain.ResponsePrimitive]

test.java

{
  "m2m:rsp":{
    "rsc":2000,
    "rqi":"1",
    "pc":{
      "m2m:ae":{
        "api":"0.2.481.1.90.T_100.2",
        "aei":"S00000000000000000005",
        "poa":["mqtt://0.2.481.1.90.T_100.2"],
        "rr":true,
        "ty":2,
        "ri":"AE00000000000000000005",
        "rn":"AE-0.2.481.1.90.T_100.2",
        "pi":"Argos",
        "ct":"20180808T041318",
        "lt":"20180822T084126",
        "lbl":[],
        "acpi":[],
        "et":"20480808T041318"
      }
    },
    "to":"0.2.481.1.90.T_100.2",
    "fr":"Argos",
    "ot":"20180824104238"
  }
}

package-info.java

public void parseString(){
    String source = ""
    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JaxbAnnotationModule());
    ResponsePrimitive responsePrimitive = mapper.readValue(source, ResponsePrimitive.class);
}

ResponsePrimitive.java

@XmlSchema(xmlns={
    @XmlNs(prefix = "m2m", namespaceURI = "http://www.onem2m.org/xml/protocols"),
    @XmlNs(prefix = "xsi", namespaceURI = "http://www.w3.org/2001/XMLSchema-instance")
})

PrimitiveContent.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "responseStatusCode", "requestIdentifier", "primitiveContent", "to", "from",
        "originatingTimestamp", "resultExpirationTimestamp", "eventCategory" })
@XmlRootElement(name = "rsp", namespace = "http://www.onem2m.org/xml/protocols")
@JsonInclude(Include.NON_EMPTY)
@JsonRootName("m2m:rsp")
public class ResponsePrimitive {
    @XmlElement(name = "rsc")
    private BigInteger responseStatusCode;
    @XmlElement(name = "rqi")
    private String requestID;
    @XmlElement(name = "pc")
    private PrimitiveContent primitiveContent;
    @XmlElement(name = "to")
    private String to;
    @XmlElement(name = "fr")
    private String from;
    @XmlElement(name = "ot")
    private String originatingTimestamp;
    @XmlElement(name = "rset")
    private String resultExpirationTimestamp;
    @XmlElement(name = "ec")
    private String eventCategory;
getter... setter...

AE.java(anyPrimitiveContent具有AE实例)

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "pc", propOrder = { "anyPrimitiveContent" })
public class PrimitiveContent {
    @XmlAnyElement(lax = true)
    private Object anyPrimitiveContent;
getter... setter...

+ edit
此更改已解析。
如何使用@XmlRootElement?

test.java

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "", propOrder = { "appID", "pointOfAccess", "requestReachability" })
@XmlRootElement(name = "ae", namespace = "http://www.onem2m.org/xml/protocols")
@JsonRootName("m2m:ae")
@JsonInclude(JsonInclude.Include.NON_EMPTY)
public class AE  {
    @XmlElement(name = "api", required = true)
    private String appID;
    @XmlElement(name = "poa")
    private List<String> pointOfAccess;
    @XmlElement(name = "rr", required = true)
    private Boolean requestReachability = true;
getter... setter...

ResponsePrimitive.java

    ObjectMapper mapper = new ObjectMapper();
    mapper.registerModule(new JaxbAnnotationModule());
    ↓(change)
    mapper.enable(DeserializationFeature.UNWRAP_ROOT_VALUE);                                 
    mapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);
    ↑
    ResponsePrimitive responsePrimitive = mapper.readValue(source, ResponsePrimitive.class);  

0 个答案:

没有答案