版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
分别创建服务端和客户端web服务项目,引入jar包:
<!-- spring jar --> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-web</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-context</artifactId> <version>${org.springframework.version}</version> </dependency> <dependency> <groupId>org.springframework</groupId> <artifactId>spring-test</artifactId> <version>${org.springframework.version}</version> <type>jar</type> <scope>test</scope> </dependency> <!-- Apache CXF WebService --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-frontend-jaxws</artifactId> <version>3.1.11</version> </dependency> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http</artifactId> <version>3.1.11</version> </dependency> <!-- Jetty is needed if you're using the CXFServlet --> <dependency> <groupId>org.apache.cxf</groupId> <artifactId>cxf-rt-transports-http-jetty</artifactId> <version>3.1.11</version> </dependency>
服务端:
添加接口:
package com.diego.service; import javax.jws.WebService; /** * 对外发布服务的接口 */ @WebService public interface HelloService { public String hello(String name); }
添加接口实现类:
package com.diego.service.impl; import com.diego.service.HelloService; public class HelloServiceImpl implements HelloService { @Override public String hello(String name) { return name+" Wolrd!"; } }
添加Spring XML配置文件:
<?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:jaxws="http://cxf.apache.org/jaxws" 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" default-lazy-init="true"> <!-- spring整合发布cxf web service 关键点 1.服务地址 2.服务类 服务完整访问地址:http:localhost:8083/server/hello --> <jaxws:server address="/hello"> <jaxws:serviceBean> <bean class="com.diego.service.impl.HelloServiceImpl"></bean> </jaxws:serviceBean> </jaxws:server> </beans>
添加web.xml配置:
<servlet> <servlet-name>cxfServlet</servlet-name> <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class> </servlet> <servlet-mapping> <servlet-name>cxfServlet</servlet-name> <url-pattern>/server/*</url-pattern> </servlet-mapping> <context-param> <param-name>contextConfigLocation</param-name> <param-value>classpath:applicationContext.xml</param-value> </context-param> <listener> <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class> </listener>
启动服务,访问http://localhost:8083/server/hello?wsdl可查看报文
客户端:
添加接口:
package com.diego.service; import javax.jws.WebService; /** * 对外发布服务的接口 */ @WebService public interface HelloService { public String hello(String name); }
添加Spring XML配置:
<?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:jaxws="http://cxf.apache.org/jaxws" 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" default-lazy-init="true"> <!-- spring整合cxf web service 客户端 1.服务地址 http:localhost:8083/server/hello 2.接口类型 --> <jaxws:client id="helloService" serviceClass="com.diego.service.HelloService" address="http://localhost:8083/server/hello?wsdl"> </jaxws:client> </beans>
添加客户端访问:
public class Client { @Test public void test() { ApplicationContext context = new ClassPathXmlApplicationContext("classpath:applicationContext.xml"); HelloService helloService = (HelloService) context.getBean("helloService"); System.out.println(helloService.getClass()); System.out.println(helloService.hello("Hello")); } }
结果:
class com.sun.proxy.$Proxy37
Hello Wolrd!===============================================
Default Suite
Total tests run: 1, Passes: 1, Failures: 0, Skips: 0
===============================================