使用Apache CXF客户端传递SOAP标头的问题

时间:2011-09-14 01:19:14

标签: java web-services soap cxf

我正在尝试为webservice实现一个简单的客户端,我面对webservice的唯一问题是,它有一个通用的端点:http://myserver3333.com:8080/ws/services,你查找部署的Web服务的方式是通过SOAP头

因此,例如,如果您尝试使用SOAP UI进行服务,

  1. 我指定的端点是:http://myserver3333.com:8080/ws/services
  2. 在SOAP标题中,我具体说明如下:
    SERVICE-NAME = MyAwesomeService
    OPERATION-NAME = makeMeMoreAwesome
  3. 那么,我怎样才能使用apache cxf client做同样的事情?

    我目前的代码:

        URL wsdlLocation = new URL("http://myserver3333.com:8080/ws/service");
    
        MyAwesomeService  service = new MyAwesomeService(wsdlLocation);
        MyAwesomeServicePort port = service.getMyAwesomeServiceSOAPPort();
    
        List<Header> headers = new ArrayList<Header>();
        Header operationNameHeader = new Header(new QName("OPERATION-NAME"), "makeMeMoreAwesome",
                                                new JAXBDataBinding(String.class));
        Header serviceNameHeader = new Header(new QName("SERVICE-NAME"), "MyAwesomeService",
                                                new JAXBDataBinding(String.class));
    
        headers.add(operationNameHeader);
        headers.add(serviceNameHeader);
    
        BindingProvider bindingProvider = (BindingProvider)port;
        bindingProvider.getRequestContext().put(Header.HEADER_LIST, headers);
    
        MakeMeMoreAwesomeRequest request = new MakeMeMoreAwesomeRequest();
        MakeMeMoreAwesomeResponse response = port.makeMeMoreAwesome(request);
    
        System.out.println(response.getAck());
    

    但是当我运行这个时,我收到了这个错误:

    Exception in thread "main" com.sun.xml.ws.wsdl.parser.InaccessibleWSDLException: 2 counts of InaccessibleWSDLException.
    
    java.io.IOException: Server returned HTTP response code: 500 for URL: http://myserver3333.com:8080/ws/services
    java.io.IOException: Server returned HTTP response code: 500 for URL: http://myserver3333.com:8080/ws/services?wsdl
    

    这是正确的,因为该位置没有WSDL,它需要遵循soap标头来获取服务。

    更新

    在@Daniel Kulp两点后我来到这里:

    1. 我添加了一个新行:bindingProvider.getRequestContext().put(BindingProvider.ENDPOINT_ADDRESS_PROPERTY, "http://myserver3333.com:8080/ws/services");
    2. 现在我收到了这个错误:

      org.apache.cxf.binding.soap.SoapFault: "http://www.myserver.com/ws/services", the namespace on the "errorMessage" element, is not a valid SOAP version.
          at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.readVersion(ReadHeadersInterceptor.java:115)
          at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:141)
          at org.apache.cxf.binding.soap.interceptor.ReadHeadersInterceptor.handleMessage(ReadHeadersInterceptor.java:60)
          at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:263)
          at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:771)
      

      我的假设是,此错误与this错误相同。但我没有使用?wsdl。那么,有什么建议吗?

1 个答案:

答案 0 :(得分:1)

2条评论:

1)你没有拿起CXF。检查类路径以确保CXF存在而不是Sun / Oracle实现。 com.sun.xml.ws.wsdl.parser.InaccessibleWSDLException显示您正在接收Sun实现。

2)传递给MyAwesomeService(wsdlLocation)的URL必须是WSDL的URL,而不是端点本身。