使用CXF发布webservice
有志者,事竟成,破釜沉舟,百二秦关终属楚。
苦心人,天不负,卧薪尝胆,三千越甲可吞吴。
笔者由于项目原因需要写接口进行交互,而接口里面最简单的就是使用webservice进行发布,由于项目架构使用的SpringMVC,那么之前说的使用JDK和AXIS2发布webservice的兼容性不是很好,CXF对于SpringMVC兼容性是最好的
采用的是 cxf-2.7.7.jar,这里就加链接了,百度上面很多,不过要积分,如果有读者需要的话可以发email给我,email:5944_zhaoxin@163.com
首先把下载下来的jar包放到WEB-INF下面的lib文件夹,这个需要注意不要在lib新建一个文件夹来专门放CXF的jar包,这样会导致CXF命名空间映射错误。
web.xml增加如下代码
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>这段代码进行发布的
接下来我webservice服务端代码如下:
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?wsdl
客户端调用的方法为:
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包由于笔者写笔记的时候离项目已经很远了,现在不太确定就不在这里阐述了