Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

java

jaxb

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?

like image 533
Andrew Avatar asked May 17 '12 14:05

Andrew


1 Answers

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

  • http://blog.bdoughan.com/2012/02/jaxb-and-package-level-xmladapters.html
like image 146
bdoughan Avatar answered Oct 07 '22 13:10

bdoughan