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);

但是生成的 类 没有任何构造函数,所以我得到了像“

这样的异常

No creator like default constructor are exists".

使用 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);

solution link