Jax Ws客户端出现意外的XML标记问题

时间:2015-08-26 23:45:09

标签: jax-ws

我正在使用供应商网络服务。使用JDK6 wsimport为供应商WSDL生成类。现在我尝试使用一个简单的java客户端类调用Web服务,并通过响应获取此异常。

意外的XML标记。预期:{http://www.abcd.com/addressValidation/} validateAddressResponse但发现:{} validateAddressResponse

供应商说试用Apache Axis生成的类,它工作正常。我注意到的是JaxWS自动将命名空间附加/绑定到请求中的每个元素,并期望响应元素中的命名空间。

生成的接口是

    @WebService(name = "AddressStandardize", targetNamespace = "http://www.abcd.com/addressValidation/")
@XmlSeeAlso({
    com.abcd.ObjectFactory.class
})
public interface AddressStandardize {

    @WebMethod(action = "http://www.abcd.com/standardizeAddress")
    @RequestWrapper(localName = "validateAddress", targetNamespace = "", className = "com.abcd.ValidateAddress")
    @ResponseWrapper(localName = "validateAddressResponse", targetNamespace = "", className = "com.abcd.ValidateAddressResponse")
    public void standardizeAddress(
    ...
    ...
    ...
    );
}

我猜Jax-WS将这个目标命名空间附加到validateAddress和validateAddressResponse元素。

如何避免将目标命名空间绑定到validateAddressResponse元素,以便它不会期望回复。请帮忙!

Jax WS生成的请求:

    <S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
    <S:Header>
        <ns2:id xmlns:ns2="http://www.abcd.com/addressValidation/">PSA</ns2:id>
    </S:Header>
    <S:Body>
        <ns2:validateAddress xmlns:ns2="http://www.abcd.com/addressValidation/">
            <arg1>
                <addresslineone>126 corbin st</addresslineone>
                <addresslinetwo/>
                <addresslinethree/>
                <city>jersey city</city>
                <state>NJ</state>
                <postalcode/>
                <country/>
                <isocountrycode>US</isocountrycode>
            </arg1>
        </ns2:validateAddress>
    </S:Body>
</S:Envelope>

退货响应:

    <soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:xyz="http://www.abcd.com/xyz">
    <soapenv:Header xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <xyz:id>PSA</xyz:id>
    </soapenv:Header>
    <soapenv:Body xmlns:soapenc="http://schemas.xmlsoap.org/soap/encoding/" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
        <validateAddressResponse xsi:noNamespaceSchemaLocation="addressValidation.xsd">
            <validateAddressReturn>
                ...
                ...
                ...
            </validateAddressReturn>
            <ResponseStatus>
                <StatusCode>SUCCESS</StatusCode>
            </ResponseStatus>
        </validateAddressResponse>
    </soapenv:Body>
</soapenv:Envelope>

此响应即将出现此例外...... 意外的XML标记。预期:{http://www.abcd.com/addressValidation/} validateAddressResponse但发现:{} validateAddressResponse

1 个答案:

答案 0 :(得分:1)

我也遇到过这个问题,并且无法找到告诉JAX-WS不要期望命名空间的方法。但是,我能够通过在SOAPHandler中预处理响应并手动将名称空间添加到XML元素(通过为其提供包含名称空间声明的新QName)来解决它。

<强> MyHandler.java:

public class MyHandler implements SOAPHandler<SOAPMessageContext> {
    @Override
    public void close(MessageContext context) { 
    }

    @Override
    public boolean handleMessage(SOAPMessageContext context) {
        //Don't intercept outbound messages (SOAP Requests)
        if ((Boolean)context.get(MessageContext.MESSAGE_OUTBOUND_PROPERTY)) {return true;}

        SOAPMessage message = context.getMessage();
        try {
            SOAPBody body = message.getSOAPBody();
            Iterator<SOAPElement> bodyIterator = body.getChildElements();
            SOAPElement responseElement = bodyIterator.next();
            responseElement.setElementQName(new QName("http://www.theserver.com/cmdb_rel_ci", origGetRecordsResponse.getLocalName(), "cmdb"));
        } catch (Exception e) {e.printStackTrace();}
        return true;
    }

    @Override
    public boolean handleFault(SOAPMessageContext context) {
        return false;
    }

    @Override
    public Set<QName> getHeaders() {
        return Collections.emptySet();
    }
}

然后我只需要在我的主类中使用以下代码将Handler添加到HandlerChain,它就可以了。

Binding binding = ((BindingProvider)port).getBinding();
List<Handler> handlers = binding.getHandlerChain();
handlers.add(new MyHandler());
binding.setHandlerChain(handlers);