Andrew
Andrew

Reputation: 837

How do I globally apply an XmlAdapter to a JAXB program?

I am using JAXB (through a mess of JaxWS and CXF) and attempting to marshal the BigDecimal type into a string (number) and int (exponent) because we now have a front end which can't interpret BigDecimal thanks to locale issues.

What I'm trying is to use an XmlAdapter to convert BigDecimal into my own type, BigDecimalUnScaled. This works fine as long as I put my @XmlJavaTypeAdapter annotation directly on the field. However, what I really would like to do is put it on my Web Service implementation and have it globally apply to all BigDecimals without having to individual marshal each return object. Is there any way I can do this?

E.g.

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've tried just putting the adapter on the implementation, but it doesn't work.

Any ideas?

Upvotes: 9

Views: 6446

Answers (1)

bdoughan
bdoughan

Reputation: 149047

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

com/example/package-info.java

@XmlJavaTypeAdapters({
    @XmlJavaTypeAdapter(value=BigDecimalAdapter.class, type=BigDecimal.class)
})
package com.example;

import javax.xml.bind.annotation.adapters.*; 

For More Information

Upvotes: 7

Related Questions