1、在项目的pom.xml中添加cxf的依赖
<!--cxf版本--> <cxf.version>3.1.6</cxf.version> <!--web service 以下都是cxf必备的--> <!--org.apache.cxf.transport.servlet.CXFServlet--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>${cxf.version}</version> </dependency> <!--不加这个包会报错Unable to locate spring NamespaceHandler for XML schema namespace [http://cxf.apache.org/jaxws]--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>${cxf.version}</version> </dependency> <!--java实现webservice,不部署到tomcat,需要jetty包支持--> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>${cxf.version}</version> </dependency>
2、写webService服务端接口
package com.hkhealthlink.webService.server; import javax.jws.WebMethod; import javax.jws.WebParam; import javax.jws.WebService; @WebService public interface MyWebService { @WebMethod(action = "sayHello") String sayHello(@WebParam(name = "str")String str); }3、写webService服务端接口实现类
package com.hkhealthlink.webService.server.impl; import javax.jws.WebService; import com.hkhealthlink.webService.server.MyWebService; @WebService(endpointInterface="com.hkhealthlink.webService.server.MyWebService", targetNamespace = "http://server.webService.hkhealthlink.com/") public class MyWebserviceImpl implements MyWebService{ @Override public String sayHello(String str) { return str; } }
4、写spring-webservice.xml
<?xml version="1.0" encoding="UTF-8"?> <beans xmlns="http://www.springframework.org/schema/beans" xmlns:context="http://www.springframework.org/schema/context" xmlns:jaxws="http://cxf.apache.org/jaxws" xmlns:soap="http://cxf.apache.org/bindings/soap" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://cxf.apache.org/jaxws http://cxf.apache.org/schemas/jaxws.xsd http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/context"> <import resource="classpath:META-INF/cxf/cxf.xml" /> <!-- 发布webservice --> <!-- webService的实现bean定义 --> <!-- web.xml配置了webservice的访问路径/server/*,那么/server/web-pulish?wsdl就是该webservice的访问路径 --> <bean id="myWebService" class="com.hkhealthlink.webService.server.impl.MyWebserviceImpl"/> <!-- jax-ws endpoint定义 --> <jaxws:endpoint id="myService" implementor="#myWebService" address="/web-publish"> </jaxws:endpoint> <!--发布webservice--> </beans>
在spring主配置文件中引入spring-webservice.xml,
<import resource="classpath:spring/spring-webservice.xml"/>
配置扫描webService包路径
5、配置web.xml<context:component-scan base-package="com.hkhealthlink.webService"/>
<!-- 配置CXF框架的核心Servlet --> <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>/server/*</url-pattern> </servlet-mapping>
6、访问http://127.0.0.1:8080/工程名/server/web-publish?wsdl
7、写cxf客户端调用,代码如下:
package com.hkhealthlink.webService.client; import org.apache.cxf.endpoint.Client; import org.apache.cxf.jaxws.endpoint.dynamic.JaxWsDynamicClientFactory; public class CxfClient { public static void main(String[] args) { JaxWsDynamicClientFactory clientFactory = JaxWsDynamicClientFactory.newInstance(); Client client = clientFactory.createClient("http://127.0.0.1:8080/XXX工程名/server/web-publish?wsdl"); Object[] result = null; try { result = client.invoke("sayHello", "hello"); System.out.println(result[0]); } catch (Exception e) { // TODO Auto-generated catch block e.printStackTrace(); } } }
注意:服务端cxf版本是3.1.6,对应的jdk要用1.8的,否则客户端调用会报错,切记!切记!