CXF建立webservice客戶端和服務端

原創做品,容許轉載,轉載時請務必以超連接形式標明文章 原始出處 、做者信息和本人聲明。不然將追究法律責任。
做者: 永恆の_☆ 地址: http://blog.csdn.net/chenghui0317/article/details/9320053

1、CXF的介紹

Apache CXF是一個開源的WebService框架,CXF大大簡化了Webservice的建立,同時它繼承了XFire的傳統,同樣能夠和spring自然的進行無縫的集成。CXF框架是一種基於servlet技術的SOA應用開發框架,要正常運用基於CXF應用框架開發的企業應用,除了CXF應用自己以外,還須要JDK和servlet容器的支持。html

2、CXF的準備條件

所須要的jar包:java

  xmlbeans-2.4.0.jarweb

  wss4j-1.5.9.jarspring

  jetty-server-7.1.6.v20100715.jarapache

  jetty-util-7.1.6.v20100715.jar
json

  geronimo-ws-metadata_2.0_spec-1.1.3.jarapi

  geronimo-activation_1.1_spec-1.1.jar服務器

  geronimo-servlet_3.0_spec-1.0.jarapp

  velocity-1.6.4.jar框架

  jaxb-xjc-2.2.1.1.jar

  xml-resolver-1.2.jar

  wsdl4j-1.6.2.jar

  cxf-2.3.0.jar

  XmlSchema-1.4.7.jar

  jaxb-api-2.2.1.jar

  jaxb-impl-2.2.1.1.jar

  neethi-2.0.4.jar

  geronimo-annotation_1.0_spec-1.1.1.jar

  geronimo-stax-api_1.0_spec-1.0.1.jar

下載地址:http://download.csdn.net/detail/ch656409110/5748183   (取本身須要的jar包)

3、建立webservice服務端

一、先將jar包放入lib目錄

二、在web.xml中配置CXF框架的核心servlet

<!-- CXF -->
	<servlet>  
	    <servlet-name>CXFServlet</servlet-name>  
	    <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>  
	    <load-on-startup>1</load-on-startup>  
	</servlet>  
	<servlet-mapping>  
	    <servlet-name>CXFServlet</servlet-name>  
	    <url-pattern>/services/*</url-pattern>  
	</servlet-mapping>

三、在applicationContext.xml中導入xml,而且發佈webservice服務。

<?xml version="1.0" encoding="UTF-8"?>
<beans
	xmlns="http://www.springframework.org/schema/beans"
	xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
	xmlns:p="http://www.springframework.org/schema/p"
	xmlns:tx="http://www.springframework.org/schema/tx"
	xmlns:aop="http://www.springframework.org/schema/aop"
	xmlns:jaxws="http://cxf.apache.org/jaxws"  
    xmlns:jaxrs="http://cxf.apache.org/jaxrs"  
	xsi:schemaLocation="http://www.springframework.org/schema/beans 
						http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
						http://www.springframework.org/schema/tx
						http://www.springframework.org/schema/tx/spring-tx-2.5.xsd
						http://www.springframework.org/schema/aop
						http://www.springframework.org/schema/aop/spring-aop-2.5.xsd
						http://cxf.apache.org/jaxws  
					    http://cxf.apache.org/schemas/jaxws.xsd  
					    http://cxf.apache.org/jaxrs  
					    http://cxf.apache.org/schemas/jaxrs.xsd
					    "
						default-autowire="byName"
						>
    <import resource="classpath:META-INF/cxf/cxf.xml" />
	<import resource="classpath:META-INF/cxf/cxf-extension-soap.xml" />
	<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
	
    <!-- <jaxws:endpoint id="facelookWebService" address="/facelookWebService" implementor="com.facelook.webservice.server.FacelookWebServiceImpl"></jaxws:endpoint> -->
    <!-- 不知道爲何,這裏的webservice配置,只能用bean來實現,不然 注入的service爲空。可是以前有個項目卻能夠,百思不得其解。。 -->
    <bean id="facelookWebService" class="com.facelook.webservice.server.FacelookWebServiceImpl"/> 
    <jaxws:endpoint id="facelookWebService1" address="/facelookWebService" implementorClass="com.facelook.webservice.server.FacelookWebServiceImpl">
    	<jaxws:implementor ref="facelookWebService"/>  
    </jaxws:endpoint>
</beans>

四、定義webservice接口FacelookWebService 和 實現類FacelookWebServiceImpl。

@WebService
public interface FacelookWebService {
	
	/**
	 * 根據傳遞的條件獲取相冊信息
	 * xml的格式規範
	 * <?xml version=\"1.0\" encoding=\"UTF-8\"?>
	 * <facelook>
	 * 	<condition>
	 * 		<name></name>
	 * 		<description></description>
	 * 		<pageno></pageno>
	 * 		<pagesize></pagesize>
	 * 	</condition>
	 * </facelook>
	 * 這裏的WebParam必須指定,不然調用的時候返回null
	 * @return
	 */
	public String getAlbumList(@WebParam(name="xmlStr") String xmlStr);
}


@WebService
//這後面的能夠不寫註釋後面的配置,在applicationContext配置也同樣(serviceName="facelookWebService",endpointInterface="com.facelook.webservice.server.FacelookWebService")
public class FacelookWebServiceImpl implements FacelookWebService{

    @Autowired
    private AlbumService albumService;
    
    @Override
    public String getAlbumList(String xmlStr) {
        try {
            List<Album> albumList = getAlbumPage(xmlStr);
            JSONArray jsonArray = JSONArray.fromObject(albumList);
            return jsonArray.toString();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }
}
這樣子,基本上就能夠了。

五、保存代碼,發佈項目,啓動tomact。

在地址欄輸入:http://localhost:8080/house/services/houseWebService?wsdl  便可看到發佈的服務端的明細。

顯示以下:


這就表示CXF發佈的webservice服務端成功了。

六、經過客戶端調用服務端webservice。

axis的客戶端訪問:

public static void main(String[] args) throws ServiceException, RemoteException, MalformedURLException {
		String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				 + " 	 <facelook>"
				 + "	 	<condition>"
				 + " 	 		<name>家</name>"
				 + "			<description></description>"
				 + "			<pageno></pageno>"
				 + "			<pagesize></pagesize>"
				 + "  		</condition>"
				 + "	 </facelook>";
		
		  Service service = new Service();
		  Call call = (Call) service.createCall();
		  call.setTargetEndpointAddress("http://localhost:8080/facelook/services/facelookWebService?wsdl");
		  QName qName = new QName("http://server.webservice.facelook.com/", "getAlbumList");
		  call.setOperationName(qName);
		  call.setUseSOAPAction(true);
		  //這下面兩行必定要加上,不然接收在服務器端收不到。
		  call.addParameter("xmlStr", XMLType.XSD_STRING, ParameterMode.IN);
		  call.setReturnType(XMLType.XSD_STRING);
		  String result = (String) call.invoke(new Object[] { xmlStr });
		  System.out.println(result);
		
		//將返回的字符串轉換成list集合
		//JSONArray array = JSONArray.fromObject(result);
		//List<Album> list = JSONArray.toList(array,Album.class);
		
	}

CXF客戶端訪問:

public static void main(String[] args) throws Exception {
		//這個是用cxf 客戶端訪問cxf部署的webservice服務
		//千萬記住,訪問cxf的webservice必須加上namespace ,不然通不過
		//如今又另一個問題,傳遞過去的參數服務端接收不到
		JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
		org.apache.cxf.endpoint.Client client = dcf.createClient("http://localhost:8080/facelook/services/facelookWebService?wsdl");
		//url爲調用webService的wsdl地址
		QName name=new QName("http://server.webservice.facelook.com/","getAlbumList");
		//namespace是命名空間,methodName是方法名
		String xmlStr = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>"
				 + " 	 <facelook>"
				 + "	 	<condition>"
				 + " 	 		<name>家</name>"
				 + "			<description></description>"
				 + "			<pageno></pageno>"
				 + "			<pagesize></pagesize>"
				 + "  		</condition>"
				 + "	 </facelook>";
		//paramvalue爲參數值
		Object[] objects=client.invoke(name,xmlStr); 
		//調用web Service//輸出調用結果
		System.out.println(objects[0].toString());
}
在這裏面傳遞的xml規範由 服務端本身規範好了,而後去解析、獲取參數,執行相應的操做,返回想要的結果給調用的客戶端。。
相關文章
相關標籤/搜索