簡體   English   中英

JaxbDto 序列化和反序列化

[英]JaxbDto Serialization and deserialization

我需要用 SOAP 接收一些消息,所以我通過xsd-scheme 和 maven-jaxb2-plugin生成了一些類,如下所示:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "Claim", propOrder = {
    "field",
})
public class ClaimType {

    @XmlElement(required = true, type = Integer.class, nillable = false)
    protected Integer field;

    public Integer getField() {
        return bpType;
    }

    public void setField(Integer value) {
        this.field= value;
    }

}

收到消息后,我需要將這些消息發送到 HashMap 包裝中的下一個微服務。 我應該使用 ObjectMapper 來轉換:

//JAXB DTO --> JSON
ObjectMapper objectMapper = new ObjectMapper();
String jsonContent = objectMapper.writeValueAsString(claimType);
map.put("json", jsonContent);

//JSON --> JAXB DTO
ObjectMapper objectMapper = new ObjectMapper();
String json = map.get("json");
ClaimType claimType = objectMapper.readValue(json, ClaimType.class);

但是生成的類沒有任何構造函數,所以我得到了像“

不存在像默認構造函數這樣的創建者”。

使用 Jaxb Dto 的最佳實踐是什么? 我可以成功將這些 json 轉換為對象嗎? 提前致謝!

我已經通過使用 ObjectMapper MixIn 解決了我的問題:

import javax.xml.bind.JAXBElement;
import javax.xml.namespace.QName;

@JsonIgnoreProperties(value = {"globalScope", "typeSubstituted", "nil"})
public abstract class JAXBElementMixIn<T> {

    @JsonCreator
    public JAXBElementMixIn(@JsonProperty("name") QName name,
            @JsonProperty("declaredType") Class<T> declaredType,
            @JsonProperty("scope") Class scope,
            @JsonProperty("value") T value) {
    }
}

和轉換:

import com.fasterxml.jackson.databind.ObjectMapper;

ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(JAXBElement.class, JAXBElementMixIn.class);

解決方案鏈接

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM