在JAXB编组中使用BigDecimal [英] Using BigDecimal in JAXB marshalling

查看:228
本文介绍了在JAXB编组中使用BigDecimal的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个带有JAXB字段注释的REST Web服务。
例如,

I have a REST webservice with JAXB fields annotations. For example,

@XmlAccessorType(XmlAccessType.PROPERTY)
public class MyClass{
  private BigDecimal sum;
  //+ getter and setter
}

如果字段sum包含大值,例如,1234567890.12345,它marshalls到1.23456789012345E9
如何编写仅用于编组此类的规则?

If field "sum" contains big value, for example, 1234567890.12345, it marshalls to 1.23456789012345E9 How to write a rule for marshalling only this class?

推荐答案

创建适配器

puclic class BigDecimalAdaptor implements XmlAdapter<String, BigDecimal>

并用于(XmlAccessType.FIELD)访问

@XmlJavaTypeAdapter(BigDecimalAdaptor.class)
private BigDecimal sum;   

(XmlAccessType.PROPERTY)访问权限

@XmlJavaTypeAdapter(BigDecimalAdaptor.class)  
public getSum()
{
   return sum;
}

适配器可以像

public class BigDecimalAdapter extends XmlAdapter<String, BigDecimal>{

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

    @Override
    public BigDecimal unmarshal(String s) throws Exception 
    {
       return new BigDecimal(s);
    }
}

这篇关于在JAXB编组中使用BigDecimal的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆