How to deserialize xml with jaxb using builder class

时光怂恿深爱的人放手 提交于 2020-04-30 09:17:06

问题


I want to deserialize xml with Jaxb and using builder class

Below is my xml input

<Test>
    <Head>Hey</Head>
    <Message code="MNP">[Hey How are yu]
        <care>test2</care>
        <care>test1</care>
    </Message>
</Test>

This Xml has mixed content. Below is classes which I used to deserialize:

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

I have added setters and getters for mixed content value, because I am not sure how to add it in builder.

Below is my test code


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

If I try to run this it's throw exception.


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

Please someone help me to deserialize with builder pattern


回答1:


Here is example of using immutable class and builder pattern with JAXB.

@XmlRootElement(name = "Root")
@XmlType(propOrder = {"head", "message"})
public class Root {

    @XmlRootElement(name = "Root")
    @XmlType(name = "RootBuilder")
    public static class Builder {
        private String head;
        private Message.Builder message;

        @XmlElement(name = "Head")
        public Builder setName(String head) {
            this.head = head;
            return this;
        }

        @XmlElement(name = "Message")
        public Builder setMessage(Message.Builder message) {
            this.message = message;
            return this;
        }

        public Root build() {
            return new Root(this.head, this.message.build());
        }
    }

    private final String head;
    private final Message message;

    Root() { // DO NOT REMOVE. This constructor is required by JAXB
        throw new UnsupportedOperationException();
    }

    Root(String head, Message message) {
        this.head = head;
        this.message = message;
    }

    @XmlElement(name = "Head")
    public String getHead() {
        return this.head;
    }

    @XmlElement(name = "Message")
    public Message getMessage() {
        return this.message;
    }

    @Override
    public String toString() {
        return "Root[head=" + this.head + ", message=" + this.message + "]";
    }
}
public class Message {

    @XmlType(name = "MessageBuilder")
    public static class Builder {
        private String code;

        @XmlAttribute
        public Builder setCode(String code) {
            this.code = code;
            return this;
        }

        public Message build() {
            return new Message(this.code);
        }
    }

    private final String code;

    Message(String code) {
        this.code = code;
    }

    @XmlAttribute
    public String getCode() {
        return this.code;
    }

    @Override
    public String toString() {
        return "Message[code=" + this.code + "]";
    }
}

Test

Root root1 = new Root.Builder()
        .setName("Hey")
        .setMessage(new Message.Builder().setCode("MNP"))
        .build();
System.out.println(root1);

StringWriter stringWriter = new StringWriter();
Marshaller marshaller = JAXBContext.newInstance(Root.class).createMarshaller();
marshaller.setProperty(Marshaller.JAXB_FRAGMENT, true);
marshaller.marshal(root1, stringWriter);
String xml = stringWriter.toString();
System.out.println(xml);

Unmarshaller unmarshaller = JAXBContext.newInstance(Root.Builder.class).createUnmarshaller();
Root.Builder rootBuilder = (Root.Builder) unmarshaller.unmarshal(new StringReader(xml));
Root root2 = rootBuilder.build();
System.out.println(root2);

Output

Root[head=Hey, message=Message[code=MNP]]
<Root><Head>Hey</Head><Message code="MNP"/></Root>
Root[head=Hey, message=Message[code=MNP]]


来源:https://stackoverflow.com/questions/61289029/how-to-deserialize-xml-with-jaxb-using-builder-class

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!