Jax-ws无法通过默认命名空间获取params

时间:2014-02-21 13:04:51

标签: java web-services namespaces jax-ws xml-namespaces

接口:

@WebService
@SOAPBinding(style = SOAPBinding.Style.DOCUMENT, use = SOAPBinding.Use.ENCODED, parameterStyle = SOAPBinding.ParameterStyle.WRAPPED)
public interface WebServ {

    @WebMethod(action = "Sum", operationName = "Sum")
    public abstract String sum(@WebParam(name = "a") int a, @WebParam(name = "b") int b);

}

实现:

@WebService(endpointInterface = "com.company.wstest.WebServ")
public class WebServImpl implements WebServ {
    @Override
    public String sum(@WebParam(name = "a") int a, @WebParam(name = "b") int b) {
        return String.valueOf(a + b);
    }
}

发布:

String endPoint = "http://localhost:" + port + "/" + env;
Endpoint endpoint = Endpoint.publish(endPoint, new WebServImpl());
if (endpoint.isPublished()) {
    System.out.println("Web service published for '" + env + "' environment");
    System.out.println("Web service url: " + endPoint);
    System.out.println("Web service wsdl: " + endPoint + "?wsdl");
}

如果我从SoapUI发送此类请求(由wsdl自动生成):

<soapenv:Envelope xmlns:soapenv="http://schemas.xmlsoap.org/soap/envelope/" xmlns:wst="http://wstest.company.com/">
   <soapenv:Header/>
   <soapenv:Body>
      <wst:Sum>
         <a>6</a>
         <b>7</b>
      </wst:Sum>
   </soapenv:Body>
</soapenv:Envelope>

我收到了正确答案:

<S:Envelope xmlns:S="http://schemas.xmlsoap.org/soap/envelope/">
   <S:Body>
      <ns2:SumResponse xmlns:ns2="http://wstest.company.com/">
         <return>13</return>
      </ns2:SumResponse>
   </S:Body>
</S:Envelope>

但实际上我需要的是使用默认命名空间发送请求:

<Envelope xmlns="http://schemas.xmlsoap.org/soap/envelope/">
   <Header/>
   <Body>
      <Sum xmlns="http://wstest.company.com/">
         <a>6</a>
         <b>7</b>
      </Sum>
   </Body>
</Envelope>

并且回复是:

...
<return>0</return>
...

我如何理解这个参数(a和b)是null ..这很奇怪,因为jax-ws解析了请求而没有错误。它看到操作,但不是参数。有人知道这是什么问题吗?

1 个答案:

答案 0 :(得分:1)

原因:&#34; a&#34;和&#34; b&#34;继承命名空间&#34; http://wstest.company.com/&#34;来自他们的父母&#34; Sum&#34;。

解决方案:在@WebService和@WebParam中设置相同的targetNamespace(为&#34使用相同的默认命名空间;&#34;,&#34; b&#34;和&#34;总和&#34):

@WebService(endpointInterface = "com.company.wstest.WebServ", targetNamespace = "http://wstest.company.com/")
public class WebServImpl implements WebServ {
    @Override
    public String sum(@WebParam(name = "a", targetNamespace = "http://wstest.company.com/") int a
                     ,@WebParam(name = "b", targetNamespace = "http://wstest.company.com/") int b) {
        return String.valueOf(a + b);
    }
}