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

百般思念 提交于 2019-12-20 20:36:07

问题


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 Classes without the Optionals. So I'm forced to make a Null-Check of every Variable.

Did anybody know how the jaxb2-maven-plugin has to be configured to use the java.util.Optional?

Thanks for you help!


回答1:


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<Integer>
{
} 

Then use this adapter in your binding

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


来源:https://stackoverflow.com/questions/38222802/how-to-generate-jaxb-classes-with-java-util-optional

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