How to globally apply an XmlAdapter to a JAXB program?

I am using JAXB (via JaxWS and CXF mess) and trying to marshal BigDecimal type in string (number) and int (exponent), because now we have a front end that BigDecimal cannot interpret thanks to the language of the questions.

I am trying to use the XmlAdapter to convert BigDecimal to my own BigDecimalUnScaled type. This works great while I put the @XmlJavaTypeAdapter annotation directly in the field. However, what I really would like to do is integrate it into my web service implementation and apply it worldwide for all BigDecimals without the need for an individual marshal of each returned object. Is there any way to do this?

eg.

Interface DummyWebService -- Get Return Object (Return object is a POJO with say an ID and a BigDecimal value) Implementation default -- No type annotation, uses default BigDecimal marshaller Marshalled Implementation -- XmlTypeAdapters implementation, should globally use BigDecimal 

I tried just putting the adapter on the implementation, but it does not work.

Any ideas?

+6
source share
1 answer

You can register your XmlAdapter at the package level and apply it to all fields / properties of this type in this package:

com / example / package-info.java

 @XmlJavaTypeAdapters({ @XmlJavaTypeAdapter(value=BigDecimalAdapter.class, type=BigDecimal.class) }) package com.example; import javax.xml.bind.annotation.adapters.*; 

Additional Information

+6
source

Source: https://habr.com/ru/post/915995/


All Articles