BigDecimal has scientific notation in soap message

心不动则不痛 提交于 2020-01-02 03:20:10

问题


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.


回答1:


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.




回答2:


@Santosh Thank you! XMLAdapter was what I needed. Furthermore as I said in my question I generate client classes with Apache CXF. In this kind of problem I had to add the following code to bindings.xjb (binding file which is use to cxf-codegen-plugin in maven).

<jaxb:javaType  name="java.math.BigDecimal" xmlType="xs:decimal"
    parseMethod="sample.BigDecimalFormater.parseBigDecimal"
    printMethod="sample.BigDecimalFormater.printBigDecimal" />

This is my formatter code:

public class BigDecimalFormater {
    public static String printBigDecimal(BigDecimal value) {
        value.setScale(5);

        return value.toPlainString();
    }

    public static BigDecimal parseBigDecimal(String value) {
        return new BigDecimal(value);
    }
}

Then this plugin generate Adapter for me

public class Adapter1 extends XmlAdapter<String, BigDecimal> {
    public BigDecimal unmarshal(String value) {
        return (sample.BigDecimalFormater.parseBigDecimal(value));
    }
    public String marshal(BigDecimal value) {
        return (sample.BigDecimalFormater.printBigDecimal(value));
    }
}

In generated class BigDecimal field has got annotation @XmlJavaTypeAdapter(Adapter1 .class), and it resolved problem.



来源:https://stackoverflow.com/questions/20115521/bigdecimal-has-scientific-notation-in-soap-message

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!