BigDecimal has scientific notation in soap message

java, soap, web-services, xsd

Solution

Here is one way this can be done.

`BigDecimal` has a constructor which takes input number as a string. This when used, preservs the input formatting when its `.toString()` method is called. e.g.

BigDecimal bd = new BigDecimal("10000000.00000");
System.out.println(bd);

will print `10000000.00000`.

This can be leveraged in `Jaxb XmlAdapters`. Jaxb XmlAdapters offer a convenient way to control/customize the marshalling/unmashalling process. A typical adapter for `BigDecimmal` would look like as follows.

public class BigDecimalXmlAdapter extends XmlAdapter{

    @Override
    public String marshal(BigDecimal bigDecimal) throws Exception {
        if (bigDecimal != null){
            return bigDecimal.toString();
        }
        else {
            return null;
        }
    }

    @Override
    public BigDecimal unmarshal(String s) throws Exception {
        try {
            return new BigDecimal(s);
        } catch (NumberFormatException e) {
            return null;
        }
    }
}

This needs to be registered with Jaxb context. Here is the link with complete example.

Problem

I have got strange problem with our webservice. I have got object OrderPosition which has got a price (which is xsd:decimal with fractionDigits = 9). Apache CXF generate proxy classes for me, and this field is BigDecimal. When I'd like to send value greater than 10000000.00000, this field in soap message has got scientific notation (for example 1.1423E+7). How can I enforce that the value has not been sent in scientific notation.

Original source