如何使用构建器类使用jaxb反序列化xml

问题描述 投票:0回答:1

我想用Jaxb并使用builder类反序列化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

[请有人帮助我使用生成器模式进行反序列化

java jaxb unmarshalling xml-deserialization
1个回答
0
投票

Test

© www.soinside.com 2019 - 2024. All rights reserved.