Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BigDecimal has scientific notation in soap message

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.

like image 552
Piotr Sobolewski Avatar asked Nov 21 '13 08:11

Piotr Sobolewski


1 Answers

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.

like image 81
Santosh Avatar answered Sep 29 '22 02:09

Santosh