java - 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'.]]
我已经调试了应用程序并确认正在调用适配器代码。我检查过的所有资源似乎都表明我的代码是正确的,所以我想知道可能是什么问题,或者我只是误解了某些东西。我也尝试过使用nillable
andrequired
属性,但没有运气。
我在这里想念什么?
解决方案
推荐阅读
- c# - 递归贝塞尔曲线算法在 C# 中不起作用
- node.js - 将 mongo dB 对象传递给快速车把
- javascript - Javascript addEventListener 仅适用于第一个元素
- xslt - 具有负值的 fo-block 填充创建问题
- python - 如何使用 Dash 制作有向图
- python - 如何将 SQLAlchemy Automap 对象与 FastApi/Pydantic 一起使用
- google-visualization - 无法删除谷歌图表的背景
- apache-flink - 无法按窗口理解事件时间组的结果
- kdb - KDB+/Q:如何在字典中找到所有匹配某个值的条目?
- sql - 合并表格中的多行?