首页 > 解决方案 > Jaxb 适配器未按预期工作

问题描述

在我们的 Spring Batch 应用程序中,我为我们的应用程序正在解组的 XML 文件的元素之一定义了 BigDecimal 适配器:

public class BigDecimalAdapter extends XmlAdapter<String, BigDecimal> {

  @Override
  public String marshal(BigDecimal v) throws Exception {
    return v != null ? v.toString() : null;
  }

  @Override
  public BigDecimal unmarshal(String v) throws Exception {
    if(v == null || "".equals(v.trim())) {
      return null;
    }
    return new BigDecimal(v.trim());
  }

}

架构如下:

<xs:element type="xs:decimal" name="COUPON_RATE" />

但是,即使定义了适配器,

@XmlElement(name = "COUPON_RATE")
@XmlJavaTypeAdapter(BigDecimalAdapter.class)
protected BigDecimal couponRate;

解析仍然失败:

[org.xml.sax.SAXParseException; lineNumber: 3; columnNumber: 620; cvc-datatype-valid.1.2.1: '' is not a valid value for 'decimal'.]]

我已经调试了应用程序并确认正在调用适配器代码。我检查过的所有资源似乎都表明我的代码是正确的,所以我想知道可能是什么问题,或者我只是误解了某些东西。我也尝试过使用nillableandrequired属性,但没有运气。

我在这里想念什么?

标签: javaspring-batchjaxb2

解决方案


推荐阅读