CXF的學習記錄

老早之前的學習的CXF,簡單的學習了一下,和Xfire類似。一直沒有記錄下來。不過貌似CXF更加強大一些,支持的協議更過。同時配置要比Xfire簡單的多。服務端提供接口和實現類就可以了。

把之前的學習做一下記錄:

需要的包在http://mirrors.cnnic.cn/apache/cxf/3.0.0-milestone1/apache-cxf-3.0.0-milestone1.zip可以下載。

CXFstart是用來啓動服務的。

public static void main(String[] args) {
		 	System.out.println("Server is starting...");  
		 	HelloDao readerService = new HelloDao();  
	        Endpoint.publish("http://localhost:8081/readerService",readerService);  
	        System.out.println("Server is started...");  
	}

Hello是接口:

@WebService
public interface Hello {
	public String sayHello(String name);
}

HelloDao是實現類:

@WebService()
public class HelloDao implements Hello {
	public String sayHello(String name) {
		return "你好: "+name;
	}
}

只需要這三個類,服務器端就好了,現在通過CXFstart來啓動服務

Server is starting...
二月 22, 2014 7:56:51 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://server.cxf.com/}HelloDaoService from class com.cxf.interFace.Hello
二月 22, 2014 7:56:52 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be http://localhost:8081/readerService
二月 22, 2014 7:56:52 下午 org.eclipse.jetty.server.Server doStart
信息: jetty-8.1.14.v20131031
二月 22, 2014 7:56:52 下午 org.eclipse.jetty.server.AbstractConnector doStart
信息: Started SelectChannelConnector@localhost:8081
二月 22, 2014 7:56:52 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromWSDL
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}Discovery from WSDL: classpath:/org/apache/cxf/ws/discovery/wsdl/wsdd-discovery-1.1-wsdl-os.wsdl
二月 22, 2014 7:56:52 下午 org.apache.cxf.endpoint.ServerImpl initDestination
信息: Setting the server's publish address to be soap.udp://239.255.255.250:3702
二月 22, 2014 7:56:52 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://docs.oasis-open.org/ws-dd/ns/discovery/2009/01}DiscoveryProxy from class org.apache.cxf.jaxws.support.DummyImpl
Server is started...

訪問wsdl的地址就是http://localhost:8081/readerService?wsdl

<wsdl:definitions xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:wsdl="http://schemas.xmlsoap.org/wsdl/" xmlns:tns="http://server.cxf.com/" xmlns:soap="http://schemas.xmlsoap.org/wsdl/soap/" xmlns:ns2="http://schemas.xmlsoap.org/soap/http" xmlns:ns1="http://interFace.cxf.com/" name="HelloDaoService" targetNamespace="http://server.cxf.com/">
<wsdl:import location="http://localhost:8081/readerService?wsdl=Hello.wsdl" namespace="http://interFace.cxf.com/"></wsdl:import>
<wsdl:binding name="HelloDaoServiceSoapBinding" type="ns1:Hello">
<soap:binding style="document" transport="http://schemas.xmlsoap.org/soap/http"/>
<wsdl:operation name="sayHello">
<soap:operation soapAction="" style="document"/>
<wsdl:input name="sayHello">
<soap:body use="literal"/>
</wsdl:input>
<wsdl:output name="sayHelloResponse">
<soap:body use="literal"/>
</wsdl:output>
</wsdl:operation>
</wsdl:binding>
<wsdl:service name="HelloDaoService">
<wsdl:port binding="tns:HelloDaoServiceSoapBinding" name="HelloDaoPort">
<soap:address location="http://localhost:8081/readerService"/>
</wsdl:port>
</wsdl:service>
</wsdl:definitions>

然後創建客戶端:


新建一個項目,爲Java

Client:

	public static void main(String[] args) {
		JaxWsProxyFactoryBean factoryBean = new JaxWsProxyFactoryBean();  
        factoryBean.setServiceClass(Hello.class);  
        factoryBean.setAddress("http://localhost:8081/readerService");  
        Hello readerService = (Hello)factoryBean.create();  
        String reader = readerService.sayHello("zhang");  
        System.out.println("Reader:"+reader);  
	}

然後運行客戶端,發現報錯
二月 22, 2014 8:38:19 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://interFace.cxf.zhang.com/}HelloService from class com.zhang.cxf.interFace.Hello
Exception in thread "main" javax.xml.ws.soap.SOAPFaultException: Unexpected wrapper element {http://interFace.cxf.zhang.com/}sayHello found.   Expected 


{http://interFace.cxf.com/}sayHello.
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:159)
at $Proxy37.sayHello(Unknown Source)
at com.zhang.cxf.Client.main(Client.java:19)
Caused by: org.apache.cxf.binding.soap.SoapFault: Unexpected wrapper element {http://interFace.cxf.zhang.com/}sayHello found.   Expected {http://interFace.cxf.com/}


sayHello.
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.unmarshalFault(Soap11FaultInInterceptor.java:84)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:51)
at org.apache.cxf.binding.soap.interceptor.Soap11FaultInInterceptor.handleMessage(Soap11FaultInInterceptor.java:40)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.interceptor.AbstractFaultChainInitiatorObserver.onMessage(AbstractFaultChainInitiatorObserver.java:113)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:69)
at org.apache.cxf.binding.soap.interceptor.CheckFaultInterceptor.handleMessage(CheckFaultInterceptor.java:34)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.endpoint.ClientImpl.onMessage(ClientImpl.java:781)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponseInternal(HTTPConduit.java:1625)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.handleResponse(HTTPConduit.java:1516)
at org.apache.cxf.transport.http.HTTPConduit$WrappedOutputStream.close(HTTPConduit.java:1323)
at org.apache.cxf.transport.AbstractConduit.close(AbstractConduit.java:56)
at org.apache.cxf.transport.http.HTTPConduit.close(HTTPConduit.java:635)
at org.apache.cxf.interceptor.MessageSenderInterceptor$MessageSenderEndingInterceptor.handleMessage(MessageSenderInterceptor.java:62)
at org.apache.cxf.phase.PhaseInterceptorChain.doIntercept(PhaseInterceptorChain.java:308)
at org.apache.cxf.endpoint.ClientImpl.doInvoke(ClientImpl.java:502)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:411)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:314)
at org.apache.cxf.endpoint.ClientImpl.invoke(ClientImpl.java:267)
at org.apache.cxf.frontend.ClientProxy.invokeSync(ClientProxy.java:96)
at org.apache.cxf.jaxws.JaxWsClientProxy.invoke(JaxWsClientProxy.java:137)


Unexpected wrapper element {http://interFace.cxf.zhang.com/}sayHello found.   Expected {http://interFace.cxf.com/}sayHello.這句話的意思就是包名不一樣,必須和服務器端的


包名統一。
將包com.zhang.cxf.interFace改爲com.cxf.interFace
輸出:
二月 22, 2014 8:40:57 下午 org.apache.cxf.wsdl.service.factory.ReflectionServiceFactoryBean buildServiceFromClass
信息: Creating Service {http://interFace.cxf.com/}HelloService from class com.cxf.interFace.Hello
Reader:你好: zhang
就OK了。這是一個簡單的應用。還有一些複雜的應用還在學習中。








發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章