如何让JAXB/MOXy反序列化XML时接受未在根节点声明的ae命名空间前缀
问题解决方法
错误根因
你遇到的SAXParseException: 前缀ae未绑定是底层XML解析器在解析XML结构阶段抛出的错误,此时还没进入JAXB/MOXy的绑定映射流程,所以你之前配置的package-info.java和OXM绑定文件的前缀映射仅作用于JAXB的对象-XML映射阶段,无法干预底层XML解析的前缀校验逻辑。
内置解决方案(无需修改原始SOAP内容)
方案1:使用MOXy专属前缀映射配置(推荐)
MOXy提供了内置的前缀映射配置项,可直接在反序列化器中声明前缀与命名空间的对应关系,无需修改XML内容:
// 导入所需类 import org.eclipse.persistence.jaxb.JAXBUnmarshallerProperties; import java.util.HashMap; import java.util.Map; // 在创建Unmarshaller后添加以下配置 Map<String, String> prefixMapping = new HashMap<>(); prefixMapping.put("ae", "urn:sbmappservices72"); umarshal1.setProperty(JAXBUnmarshallerProperties.NAMESPACE_PREFIX_MAPPER, prefixMapping);
方案2:直接反序列化完整SOAP报文(最省事)
你无需单独提取业务节点反序列化,仅需新增两个极简的SOAP结构映射类,直接对完整SOAP报文反序列化即可,根节点本来就存在ae前缀声明,完全不会触发前缀绑定错误:
新增SOAP结构映射类:
import javax.xml.bind.annotation.XmlAccessType; import javax.xml.bind.annotation.XmlAccessorType; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Envelope", namespace = "http://schemas.xmlsoap.org/soap/envelope/") @XmlAccessorType(XmlAccessType.FIELD) public class SoapEnvelope { @XmlElement(name = "Body", namespace = "http://schemas.xmlsoap.org/soap/envelope/") private SoapBody body; public SoapBody getBody() { return body; } public void setBody(SoapBody body) { this.body = body; } } @XmlAccessorType(XmlAccessType.FIELD) public class SoapBody { @XmlElement(name = "GetItemsByQueryResponse", namespace = "urn:sbmappservices72") private QueryResponse queryResponse; public QueryResponse getQueryResponse() { return queryResponse; } public void setQueryResponse(QueryResponse queryResponse) { this.queryResponse = queryResponse; } }
修改反序列化代码直接读取完整SOAP文件即可:
SoapEnvelope envelope = (SoapEnvelope) umarshal1.unmarshal(new File("c:\\temp\\完整soap响应.xml")); QueryResponse qresp = envelope.getBody().getQueryResponse();
方案3:JDK原生自定义NamespaceContext
如果不想依赖MOXy专属特性,可自定义命名空间上下文传递给XML解析器,使用SAXSource进行反序列化:
import org.xml.sax.InputSource; import org.xml.sax.XMLReader; import javax.xml.transform.sax.SAXSource; import org.xml.sax.helpers.XMLReaderFactory; import javax.xml.namespace.NamespaceContext; import java.util.Iterator; // 自定义NamespaceContext实现 class CustomNamespaceContext implements NamespaceContext { @Override public String getNamespaceURI(String prefix) { if ("ae".equals(prefix)) { return "urn:sbmappservices72"; } return null; } @Override public String getPrefix(String namespaceURI) { if ("urn:sbmappservices72".equals(namespaceURI)) { return "ae"; } return null; } @Override public Iterator<String> getPrefixes(String namespaceURI) { return null; } } // 反序列化时代码调整 XMLReader xmlReader = XMLReaderFactory.createXMLReader(); xmlReader.setProperty("http://apache.org/xml/properties/namespace-context", new CustomNamespaceContext()); SAXSource source = new SAXSource(xmlReader, new InputSource(new FileInputStream("c:\\temp\\ae_egress.xml"))); QueryResponse qresp = (QueryResponse) umarshal1.unmarshal(source);
需要修正的配置错误
你现有的配置存在两个低级错误,会导致即使解决了前缀问题也无法正常映射:
package-info.java中namespace属性多写了逗号,错误值为urn:sbm,appservices72,修正为urn:sbmappservices72- 所有
@XmlRootElement、@XmlElement注解不要带ae:前缀,JAXB映射仅识别命名空间,前缀只是别名,你应该给注解配置namespace属性而非把前缀写在name里,正确示例:
// 错误写法 @XmlRootElement(name="ae:GetItemsByQueryResponse") // 正确写法 @XmlRootElement(name="GetItemsByQueryResponse", namespace = "urn:sbmappservices72")
内容的提问来源于stack exchange,提问作者user187325
相关产品推荐
相关产品推荐

