I am using Spring 3 IOC and JAXB / JAX-WS in the WebService that I wrote. Now I have a small problem with the data, which must be rounded before returning to the consumer, since they are not able to handle the full accuracy of the values.
To minimize the impact on WS design and calculations, I decided to use the Jaxb XmlAdapter to round the values ββwhen sorting my answer. Everything is working fine.
Now my problem is that I would like to make it flexible. That is: in some cases I need to round to two decimal places, in some 4, etc. Right now, I have to create TwoDecimalAdapter and FourDecimalAdapter and use the appropriate, when necessary, in my model definitions. This means code duplication.
Do I need to create a common Rounding Adapter and pass a parameter to it? For example, instead of:
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=FourDecimalRoundingAdapter.class,type=java.math.BigDecimal.class)
I would like to do something like:
@javax.xml.bind.annotation.adapters.XmlJavaTypeAdapter(value=new RoundingAdapter(4),type=java.math.BigDecimal.class)
Obviously this does not work, since JAXB instantiates the adapter itself, but is there any method I can use to pass parameters to the adapter? I would like to declare a rounding adapter in Spring and use it that way, but again, I cannot develop a reusable solution.
Thanks,
Eric
Eric B.
source share