升级org.apache.ws.commons.axiom:axiom-impl至1.4.0后OMAttributeImpl缺失的解决办法
解决方案:改用Axiom工厂API创建OMAttribute实例
OMAttributeImpl是Axiom的内部实现类,原本就不应该在业务代码中直接实例化。1.4.0版本的内部重构移除了该类,因此需要改用Axiom官方提供的标准工厂方法来创建属性实例。
修改后的代码如下:
if (element != null) { OMFactory factory = element.getOMFactory(); // 直接创建指定本地名称和值的属性 OMAttribute attribute = factory.createAttribute("localName", "1"); element.addAttribute(attribute); }
如果需要对属性的命名空间进行更精细的控制,可以使用重载的createAttribute方法:
if (element != null) { OMFactory factory = element.getOMFactory(); // 指定前缀、本地名称、命名空间URI和属性值 OMAttribute attribute = factory.createAttribute("prefix", "localName", "namespaceURI", "1"); element.addAttribute(attribute); }
说明
Axiom采用工厂模式封装内部实现细节,通过OMFactory创建实例是符合框架设计规范的做法,既能避免依赖内部实现类导致的版本兼容问题,也能保证代码符合Axiom的使用最佳实践。
内容的提问来源于stack exchange,提问作者Clueless
相关产品推荐
相关产品推荐

