使用CXF發佈webservice

有志者,事竟成,破釜沉舟,百二秦關終屬楚。
苦心人,天不負,臥薪嚐膽,三千越甲可吞吳。
java

 

  筆者因爲項目緣由須要寫接口進行交互,而接口裏面最簡單的就是使用webservice進行發佈,因爲項目架構使用的SpringMVC,那麼以前說的使用JDK和AXIS2發佈webservice的兼容性不是很好,CXF對於SpringMVC兼容性是最好的web

  採用的是 cxf-2.7.7.jar,這裏就加連接了,百度上面不少,不過要積分,若是有讀者須要的話能夠發email給我,email:5944_zhaoxin@163.comspring

 

  首先把下載下來的jar包放到WEB-INF下面的lib文件夾,這個須要注意不要在lib新建一個文件夾來專門放CXF的jar包,這樣會致使CXF命名空間映射錯誤。apache

  web.xml增長以下代碼spring-mvc

1   <servlet>
2         <servlet-name>CXFServlet</servlet-name>
3         <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
4         <load-on-startup>1</load-on-startup>
5     </servlet>
6     <servlet-mapping>
7         <servlet-name>CXFServlet</servlet-name>
8         <url-pattern>/webService/*</url-pattern>
9     </servlet-mapping>

  spring-mvc.xml增長以下代碼,背景色爲紅色的是新增長的架構

 1 <?xml version="1.0" encoding="UTF-8"?>
 2 <beans xmlns="http://www.springframework.org/schema/beans"
 3     xmlns:mvc="http://www.springframework.org/schema/mvc" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
 4     xmlns:p="http://www.springframework.org/schema/p" xmlns:context="http://www.springframework.org/schema/context"
 5     xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:jaxws="http://cxf.apache.org/jaxws"
 6     xmlns:cxf="http://cxf.apache.org/core"
 7     xsi:schemaLocation="http://www.springframework.org/schema/beans
 8         http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
 9         http://www.springframework.org/schema/context
10         http://www.springframework.org/schema/context/spring-context-3.1.xsd
11         http://cxf.apache.org/core http://cxf.apache.org/schemas/core.xsd
12         http://cxf.apache.org/jaxrs http://cxf.apache.org/schemas/jaxrs.xsd
13         http://cxf.apache.org/jaxws
14         http://cxf.apache.org/schemas/jaxws.xsd  
15         http://www.springframework.org/schema/mvc
16         http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd 
17         ">
18    <!-- 引入CXF配置文件,低版本還需引入其餘兩個文件 -->
19     <import resource="classpath*:META-INF/cxf/cxf.xml" />
20     <import resource="classpath*:META-INF/cxf/cxf-servlet.xml" />   
21     
22     <!--  配置方式2    注意:implementor爲接口的具體實現類 -->
23     <jaxws:endpoint implementor="com.hsinfo.web.pageoffice.action.WebServiceImpl" address="/moblieWebService" ></jaxws:endpoint>

發佈方式就是<jaxws:endpoint implementor="com.hsinfo.web.pageoffice.action.WebServiceImpl" address="/moblieWebService" ></jaxws:endpoint>這段代碼進行發佈的mvc

接下來我webservice服務端代碼以下:app

 1 package com.hsinfo.web.pageoffice.action;
 2 
 3 import java.util.List;
 4 
 5 import javax.jws.WebMethod;
 6 import javax.jws.WebParam;
 7 
 8 
 9 public interface WebService {
10     
11     @WebMethod
12     public String mobileSubmit(@WebParam List<String> list);
13 
14     @WebMethod
15     public String mobileCheckWord(@WebParam List<String> list);
16 
17     @WebMethod
18     public String mobileGoBack(@WebParam  List<String> list);
19 }

 

 1 package com.hsinfo.web.pageoffice.action;
 2 
 3 import java.util.List;
 4 import java.util.Map;
 5 
 6 import javax.jws.WebMethod;
 7 import javax.xml.ws.Endpoint;
 8 
 9 import org.springframework.context.ApplicationContext;
10 import org.springframework.context.support.ClassPathXmlApplicationContext;
11 
12 import com.hsinfo.web.system.service.SystemService;
13 
14 @javax.jws.WebService
15 public class WebServiceImpl implements WebService{
16 
17     @Override
18     @WebMethod
19     public String mobileSubmit(List<String> list) {
20         ApplicationContext contex=new ClassPathXmlApplicationContext("spring*.xml");
21         SystemService systemService = contex.getBean(SystemService.class);
22         
23         String message = new MobileWebServiceAction().mobileSubmitImpl(list,systemService);
24         return message;
25     }
26     @Override
27     @WebMethod
28     public String mobileCheckWord(List<String> list) {
29         ApplicationContext contex=new ClassPathXmlApplicationContext("spring*.xml");
30         SystemService systemService = contex.getBean(SystemService.class);
31         
32         String message = new MobileWebServiceAction().mobileCheckWordImpl(list,systemService);
33         return message;
34     }
35     @Override
36     @WebMethod
37     public String mobileGoBack(List<String> list) {
38         ApplicationContext contex=new ClassPathXmlApplicationContext("spring*.xml");
39         SystemService systemService = contex.getBean(SystemService.class);
40         
41         String message = new MobileWebServiceAction().goBackOne(list,systemService);
42         return message;
43     }
44 }

  以上就是服務端的代碼,發佈的地址爲:http://10.6.180.5:8108/lhk/webService/moblieWebService?wsdlide

 

  客戶端調用的方法爲:url

 1      JaxWsDynamicClientFactory dcf = JaxWsDynamicClientFactory.newInstance();
 2         String Ipname = idsList.get(9).toString();
 3         String wsUrl = "http://10.6.180.5:8108/lhk/webService/moblieWebService?wsdl"; 
 4         Client client = dcf.createClient(wsUrl);
 5         String method = "mobileSubmit";//webservice的方法名 
 6         idsList.remove(idsList.size()-1);
 7         Object[] result = null;
 8         try {
 9            result = client.invoke(method, idsList);//調用webservice 
10            System.out.println(result[0]);
11         } catch (Exception e) {
12            e.printStackTrace();
13         }

 

  至於須要導入的cxf最少的jar包因爲筆者寫筆記的時候離項目已經很遠了,如今不太肯定就不在這裏闡述了

相關文章
相關標籤/搜索