【问题标题】:Unmarshall SOAP fault JAXB解组 SOAP 错误 JAXB
【发布时间】:2018-12-31 08:25:21
【问题描述】:

我需要帮助来使用 JAXB java 类解组 SOAP 响应

 <soapenv:Fault>
      <faultcode>soapenv:Server</faultcode>
      <faultstring>DLResponseException</faultstring>
      <detail>...
     ....

我创建了一个 java 类,其根元素如下

@XmlRootElement(name="Fault", namespace="http://schemas.xmlsoap.org/soap/envelope/")
public class MFaultResponse{
..
}

用法:

 InputStream is = new ByteArrayInputStream(sample.getBytes());

    JAXBContext jaxbContext = JAXBContext.newInstance(MFaultResponse.class);  
    Unmarshaller jaxbUnmarshaller = jaxbContext.createUnmarshaller();  

    MFaultResponse response= (MFaultResponse) jaxbUnmarshaller.unmarshal(is);
    System.out.println(response.getFaultCode());

例外:

原因:org.xml.sax.SAXParseException;行号:1;列号:16;元素“soapenv:Fault”的前缀“soapenv”未绑定。

【问题讨论】:

    标签: spring jaxb spring-integration


    【解决方案1】:

    这个问题与 Spring Integration 无关,也与 Spring 无关。

    这是典型的 JAXB 情况,当您有一个 xml 片段时。我知道这是 SOAP 响应的主体,但正如您所见,soapenv 的真正命名空间是在 SOAP 信封的根上声明的。

    如果你没有那个前缀,你可以解组文档,但因为它是关于一个片段,而不是整个 SOAP 响应,我建议做手动 XPath 来提取这 3 个标签'值并手动构建这样的MFaultResponse

    但不确定,为什么 Spring WS 中的标准 SoapFault 方法对您不起作用..

    【讨论】:

    • 对不起,我的错。我再次打印了输出,它也带有模式 schemas.xmlsoap.org/soap/envelope\">。所以用@XmlRootElement(name="Fault", namespace="schemas.xmlsoap.org/soap/envelope/") 更新了代码,它可以工作了。
    • 嗯,很高兴听到,但我仍然相信我的回答是相关的并且可以被接受
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-10-02
    • 1970-01-01
    • 2023-04-03
    • 1970-01-01
    • 2023-03-26
    相关资源
    最近更新 更多