Jaxb Adapter not working as expected

95 Views Asked by At

On our Spring Batch application, I've defined a BigDecimal adapter for one of the elements of a XML file that our application is unmarshaling :

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());
  }

}

And the schema as follows:

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

But, even with the adapter defined,

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

the parsing is still failing:

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

I've debugged the application and confirmed that the adapter code is being invoked. All the resources I've checked seem to indicate that my code is correct, so I'm wondering what might be the problem or if I just misinterpreted something. I've also tried to play with the nillable and required properties, but with no luck.

What am I missing here?

0

There are 0 best solutions below