CXF 客户端和 WS-Addressing 属性

Posted

技术标签:

【中文标题】CXF 客户端和 WS-Addressing 属性【英文标题】:CXF Client and WS-Addressing attributes 【发布时间】:2016-01-31 07:50:00 【问题描述】:

我尝试使用以下客户端代码调用一个网络服务:

    import com.test.wsdl.CxfAdd;
    import com.test.wsdl.CxfAddService;

    import java.util.Map;

    import javax.xml.ws.BindingProvider;

    import org.apache.cxf.ws.addressing.AddressingProperties;
    import org.apache.cxf.ws.addressing.AttributedURIType;
    import org.apache.cxf.ws.addressing.EndpointReferenceType;
    import org.apache.cxf.ws.addressing.JAXWSAConstants;

    public class Main 

        public static void main(String[] args) 

            CxfAddService service = new CxfAddService();
            CxfAdd client = service.getCxfAddPort();

            Map<String, Object> requestContext = ((BindingProvider)client).getRequestContext();

            AddressingProperties maps = new AddressingProperties();
            EndpointReferenceType ref = new EndpointReferenceType();
            AttributedURIType add = new AttributedURIType();

            add.setValue("http://www.w3.org/2005/08/addressing/anonymous");
            ref.setAddress(add);
            maps.setReplyTo(ref);
            maps.setFaultTo(ref);

            requestContext.put(JAXWSAConstants.CLIENT_ADDRESSING_PROPERTIES, maps);

            client.myMethodOneWay("Input message");
        
    

服务器端(Tomcat)上,webservice 实现如下:

CxfAdd.java:

    package com.test.ws;

    import javax.jws.Oneway;
    import javax.jws.WebResult;
    import javax.jws.WebService;
    import javax.xml.ws.soap.Addressing;

    @WebService(targetNamespace = "http://test.com/wsdl")
    @Addressing(enabled = true, required = true)
    public interface CxfAdd 

        @WebResult(name = "response")
        public abstract String myMethod(String message);

        @WebResult(name="response")
        @Oneway
        public void myMethodOneWay(String message);
    

CxfAddImpl.java:

    package com.test.ws;

    import javax.annotation.Resource;
    import javax.jws.Oneway;
    import javax.jws.WebService;
    import javax.xml.ws.WebServiceContext;
    import javax.xml.ws.soap.Addressing;

    @WebService
    @Addressing
    public class CxfAddImpl implements CxfAdd 

        @Resource
        WebServiceContext webServiceContext;

        public String myMethod(String message) 
            System.out.println("Invoking sayHello in " + getClass());
            return "Hello " + message;
        

        @Oneway
        public void myMethodOneWay(String message) 
            // TODO Auto-generated method stub      
        
    

但是,当我运行客户端代码时,在服务器端出现以下错误:

INFO: Inbound Message
----------------------------
ID: 46
Address: http://localhost:8080/CxfAddressingServer/services/cxfadd
Encoding: UTF-8
Http-Method: POST
Content-Type: text/xml; charset=UTF-8
Headers: Accept=[*/*], cache-control=[no-cache], connection=[keep-alive], Content-Length=[211], content-type=[text/xml; charset=UTF-8], host=[localhost:8080], pragma=[no-cache], SOAPAction=[""], user-agent=[Apache CXF 3.1.3]
Payload: <soap:Envelope xmlns:soap="http://schemas.xmlsoap.org/soap/envelope/"><soap:Body><ns2:myMethodOneWay xmlns:ns2="http://test.com/wsdl"><arg0>Input message</arg0></ns2:myMethodOneWay></soap:Body></soap:Envelope>
--------------------------------------
Out 30, 2015 7:18:56 PM org.apache.cxf.ws.addressing.ContextUtils retrieveMAPs
WARNING: WS-Addressing - failed to retrieve Message Addressing Properties from context

似乎我没有发送 ws-addressing 属性,谁能帮我找出我的代码中有什么问题或缺失?谢谢。

【问题讨论】:

【参考方案1】:

在客户端代码中,我替换了以下代码:

CxfAdd client = service.getCxfAddPort();

以下内容:

CxfAdd client = service.getCxfAddPort(
                      new org.apache.cxf.ws.addressing.WSAddressingFeature());

现在可以使用了。

这里的其他人提供了解决方案:

http://www.coderanch.com/t/657413/Web-Services/java/CXF-Client-WS-Addressing-attributes

【讨论】:

这个我找了很多,非常感谢!

以上是关于CXF 客户端和 WS-Addressing 属性的主要内容,如果未能解决你的问题,请参考以下文章

CXF:通过现有的 Servlet 传输提供解耦端点

axis2与cxf区别

Apache CXF - WS 解决如何从标题中删除/删除回复

Apache CXF - WS 解决如何设置 From、ReplyTo、Headers

使用 Spring-WS 路由 WS-Addressing 响应

如何使用 JAX-WS 客户端指定 WS-Addressing 版本?