【发布时间】:2020-04-18 12:24:07
【问题描述】:
我想用 Jaxb 反序列化 xml 并使用构建器类
下面是我的xml输入
<Test>
<Head>Hey</Head>
<Message code="MNP">[Hey How are yu]
<care>test2</care>
<care>test1</care>
</Message>
</Test>
此 Xml 包含混合内容。 下面是我用来反序列化的类:
@XmlRootElement(name = "Test")
public class Test {
private final String header;
private final List<Message> message;
private Test(final Builder builder) {
this.header = builder.header.orElse(null);
this.message = builder.message;
}
public static Builder builder() {
return new Builder();
}
//getters
public static final class Builder {
private Optional<String> header = Optional.empty();
private List<Message> message = List.of();
@XmlElement(name= "Head")
public Builder withHeader(final String theHeader) {
this.header = Optional.ofNullable(theHeader);
return this;
}
@XmlElement(name = "message")
public Builder withMessage(final List<Message> theMessages) {
this.message = theMessages;
return this;
}
public Test build() {
return new Test(this);
}
}
}
public class Message {
private final String code;
private final List<String> description;
private List<Object> mixedContent;
private Message(final Builder builder) {
this.code = builder.code.orElse(null);
this.description = builder.description;
}
public static Builder builder() {
return new Builder();
}
//getters
@XmlMixed
public List<Object> getHeader() {
return this.mixedContent;
}
public void setHeader(final List<Object> mixedContent) {
this.mixedContent = mixedContent;
}
@XmlTransient
public String getText() {
if (this.mixedContent == null)
return null;
final String text = this.mixedContent.stream()
.filter(obj -> obj instanceof String)
.map(String::valueOf)
.collect(Collectors.joining());
return text.trim();
}
public static final class Builder {
private Optional<String> code = Optional.empty();
private List<String> description = List.of();
private Builder() {
}
@XmlAttribute
public Builder withCode(final String theCode) {
this.code = Optional.ofNullable(theCode);
return this;
}
@XmlElement(name = "care")
public Builder withDescription(final List<String> theDescription) {
this.description =theDescription;
return this;
}
public Message build() {
return new Message(this);
}
}
}
我已经为混合内容值添加了 setter 和 getter,因为我不确定如何在 builder 中添加它。
下面是我的测试代码
String input = "<Test>\n"
+ " <Head>Hey</Head>\n"
+ " <Message code=\"MNP\">[Hey How are yu]\n"
+ " <care>test2</care>\n"
+ " <care>test1</care>\n"
+ " </Message>\n"
+ "</Test>";
JAXBContext jaxbContext = JAXBContext.newInstance(Test.class);
Unmarshaller unmarshaller = jaxbContext.createUnmarshaller();
Test test = (Test) unmarshaller.unmarshal(new StringReader(input));
System.out.println(test);
如果我尝试运行它,则会抛出异常。
Exception in thread "main" com.sun.xml.bind.v2.runtime.IllegalAnnotationsException: 1 counts of IllegalAnnotationExceptions
com.example.builder.Test does not have a no-arg default constructor.
this problem is related to the following location:
at com.example.builder.Test
请有人帮我用构建器模式反序列化
【问题讨论】:
-
重复问题:您已经在这里问过如何处理混合内容:how to deserialize xml element without tag using jackson,已回答( “使用 JAXB”以及如何使用的示例)。这个问题读起来完全一样,即作为一个关于如何制作混合内容的问题,你得到了答案:你不能使用 Jackson,因为 Jackson 不支持它。
-
如果这应该是关于如何将 JAXB 与 builders 一起使用的问题,那么 问那个,并从问题,因为这只会使问题复杂化。
-
@Andreas,我已经编辑了我的问题
-
调用 JAXB 的代码在哪里?你得到什么错误?请阅读:How do I ask a good question?
-
您希望 JAXB 填写 builder,所以应该用
@XmlRootElement(name = "Test")注释的是Test.Builder,而您输入给 @ 的是Test.Builder.class987654331@。 JAXB 应该对Test和Message的解组一无所知,只有Test.Builder和Message.Builder。一旦 JAXB 用数据填充了构建器,您调用build()方法。
标签: java jaxb unmarshalling xml-deserialization