How to generate JaxB-Classes with java.util.Optional?

后端 未结 1 1588
萌比男神i
萌比男神i 2021-02-08 05:13

I use the jaxb2-maven-plugin to generate JaxB-Classes from a given XSD. It works fine. But now I want to use java.util.Optional in the generated Classes. But JaxB generated the

1条回答
  •  温柔的废话
    2021-02-08 05:29

    Maybe you could find something more generic but i'm not sure if this is possible. Anyway you can still define custom Adapter for types you want to be optional.

    Here is an example of Integer

    First, create an Adapter

    public final class IntegerOptionalAdapter extends OptionalAdapter
    {
    } 
    

    Then use this adapter in your binding

    @XmlAttribute
    @XmlJavaTypeAdapter(IntegerOptionalAdapter.class)
    private Optional someInteger;
    

    0 讨论(0)
提交回复
热议问题