[英]JaxbDto Serialization and deserialization
I need to receive some message with SOAP so I've generated a few classes by xsd-scheme and maven-jaxb2-plugin like this:我需要用 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;
}
}
After receiving message I need to send these to the next one microservice in wrap of HashMap.收到消息后,我需要将这些消息发送到 HashMap 包装中的下一个微服务。 I supposed to use ObjectMapper to convert:
我应该使用 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);
But the generated classes are haven't any constructors so I got the exception like "但是生成的类没有任何构造函数,所以我得到了像“
No creator like default constructor are exists".
不存在像默认构造函数这样的创建者”。
What is the best preactice to work with Jaxb Dto?使用 Jaxb Dto 的最佳实践是什么? Can I do smth to successful convert these json to object?
我可以成功将这些 json 转换为对象吗? Thanks in advance!
提前致谢!
I've solved my problem by using ObjectMapper MixIn:我已经通过使用 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) {
}
}
And the convertation:和转换:
import com.fasterxml.jackson.databind.ObjectMapper;
ObjectMapper objectMapper = new ObjectMapper();
objectMapper.addMixIn(JAXBElement.class, JAXBElementMixIn.class);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.