SOAP消息无法解析为java对象

时间:2016-06-13 13:24:06

标签: java xml soap jaxb

我有以下SOAP消息并生成了jaxb类。

SOAP消息:

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<soapenv:Body>
<ns1:sendSmsResponse xmlns:ns1="http://www.csapi.org/schema/parlayx/sms/send/v2_2/local">
<result>100001200301111029065e4000141</result>
</ns1:sendSmsResponse>
</soapenv:Body>
</soapenv:Envelope>

JAXB类

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sendSmsResponse", propOrder = {
    "result"
})
public class SendSmsResponse {
    @XmlElement(required = true)
    protected String result;
    public String getResult() {
        return result;
    }
    public void setResult(String value) {
        this.result = value;
    }

}

但是这会产生如下的解组异常。

javax.xml.ws.soap.SOAPFaultException: Unmarshalling Error: unexpected element (uri:"http://www.csapi.org/schema/parlayx/sms/send/v2_2/local", local:"result"). Expected elements are <{}result> 
    at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:161)

但如果我从ns1:中移除<ns1:result>,它就会解析。可能是什么原因?

1 个答案:

答案 0 :(得分:1)

错误消息告诉您遇到带有命名空间的结果元素,而它期望没有命名空间的结果元素(&lt; {} result&gt;中的&#34; {}&#34;表示没有命名空间)。

您必须通过命名空间属性为JAXB指定命名空间:

@XmlAccessorType(XmlAccessType.FIELD)
@XmlType(name = "sendSmsResponse", propOrder = {
    "result"
})
public class SendSmsResponse {
    @XmlElement(required = true,namespace="http://www.csapi.org/schema/parlayx/sms/send/v2_2/local")
    protected String result;
    public String getResult() {
        return result;
    }
    public void setResult(String value) {
        this.result = value;
    }

}