JAXB: Isn't it possible to use an XmlAdapter without @XmlJavaTypeAdapter?

前端 未结 2 888
耶瑟儿~
耶瑟儿~ 2020-12-30 00:16

Can\'t I register a bunch of XmlAdapters to Marshaller|Unmarshaller so that I wouldn\'t need to specify @XmlJavaTypeAdapter

2条回答
  •  忘掉有多难
    2020-12-30 00:43

    You can use the package-info.java

    That's called "package level".

    Example : put a package-info.java in the same package that the class you want to marshall/unmarshall.

    Suppose you have a class mypackage.model.A and an adapter CalendarAdapter in mypackage.adapter. Declare a package-info.java file in mypackage.model containing :

    @XmlJavaTypeAdapters({
        @XmlJavaTypeAdapter(value = CalendarAdapter.class, type = Calendar.class)
    })
    package mypackage.model;
    
    import java.util.Calendar;
    import mypackage.adapter.CalendarAdapter;
    

    All field of type Calendar in the class A will be marshalled or unmarshalled with the CalendarAdapter.

    You can find useful informations there : http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html

提交回复
热议问题