一个CXF集成SPRING的WEBSERVICE完整实例
Posted 刀刀(2把刀)
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了一个CXF集成SPRING的WEBSERVICE完整实例相关的知识,希望对你有一定的参考价值。
1 首先准备以下JAR包
- activation.jar
- commons-logging-1.1.1.jar
- cxf-2.5.6.jar
- jaxb-api-2.2.1.jar
- jaxb-impl-2.1.3.jar
- jaxws-api-2.2.8.jar
- neethi-3.0.2.jar
- ow2-jws-2.0-spec-1.0.11.jar
- spring-aop-3.0.5.RELEASE.jar
- spring-asm-3.0.5.RELEASE.jar
- spring-beans-3.0.5.RELEASE.jar
- spring-context-3.0.5.RELEASE.jar
- spring-context-support-3.0.5.RELEASE.jar
- spring-core-3.0.5.RELEASE.jar
- spring-expression-3.0.5.RELEASE.jar
- spring-oxm-3.0.5.RELEASE.jar
- spring-web-3.0.5.RELEASE.jar
- spring-ws-core-2.0.2.RELEASE.jar
- spring-ws-security-2.0.2.RELEASE.jar
- spring-xml-1.5.9.jar
- stax-api-1.0.jar
- stax2-api-3.1.1.jar
- webservices-api-2.2.0-1.jar
- webservices-extra-2.1-20.jar
- woodstox-core-asl-4.1.2.jar
- ws-test.jar
- wsdl4j-1.6.2.jar
- wss4j-1.6.7.jar
- xmlschema-core-2.0.3.jar
2 编写服务端
2.1 新建一个空的WEB工程,我这里命名为ws_test,将其放置于TOMCAT的webapps目录;
2.2 然后在ws_test下面建立WEB-INF文件夹,并在WEB-INF文件夹中建立目录lib;
2.3 将上面提到的JAR包都拷贝到lib目录中;
2.4 在WEB-INF目录中新建web.xml文件,并将如下内容放于其中:
- <?xml version="1.0" encoding="ISO-8859-1"?>
- <web-app xmlns="http://java.sun.com/xml/ns/javaee"
- xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
- version="2.5">
- <description>
- Web Service Test.
- </description>
- <display-name>Web Service Test</display-name>
- <listener>
- <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
- </listener>
- <context-param>
- <param-name>contextConfigLocation</param-name>
- <param-value>classpath*:applicationContext-server.xml</param-value>
- </context-param>
- <listener>
- <listener-class>org.springframework.web.util.IntrospectorCleanupListener</listener-class>
- </listener>
- <servlet>
- <servlet-name>CXFService</servlet-name>
- <servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
- </servlet>
- <servlet-mapping>
- <servlet-name>CXFService</servlet-name>
- <url-pattern>/*</url-pattern>
- </servlet-mapping>
- </web-app>
这个配置文件是WEB应用的核心配置文件,如Listener的配置,servlet请求的映射等,这里并配置了启动的时候加载SPRING,并初使化我们的WEBSERVICE服务端配置文件。
2.5 在ECLIPSE中新建JAVA工程,并在工程中新建lib目录,将上面提到的JAR包全部放到lib目录,并将他们全部加入到classpath中;
2.6 增加服务端的JAVA类
这个配置文件是WEB应用的核心配置文件,如Listener的配置,servlet请求的映射等,这里并配置了启动的时候加载SPRING,并初使化我们的WEBSERVICE服务端配置文件。
2.5 在ECLIPSE中新建JAVA工程,并在工程中新建lib目录,将上面提到的JAR包全部放到lib目录,并将他们全部加入到classpath中;
2.6 增加服务端的JAVA类
- package com.use.test.ws.server;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import javax.jws.soap.SOAPBinding;
- import javax.jws.soap.SOAPBinding.Style;
- import com.use.test.ws.entity.User;
- /**
- * <b>function:</b>定制客户端请求WebService所需要的接口
- */
- @WebService
- @SOAPBinding(style = Style.RPC)
- public interface IService {
- public User getUserByName(@WebParam(name = "name") String name);
- public void setUser(User user);
- }
- package com.use.test.ws.server;
- import java.util.Date;
- import javax.jws.WebParam;
- import javax.jws.WebService;
- import javax.jws.soap.SOAPBinding;
- import javax.jws.soap.SOAPBinding.Style;
- import com.use.test.ws.entity.User;
- /**
- * <b>function:</b> WebService传递复杂对象,如JavaBean、Array、List、Map等
- */
- @WebService
- @SOAPBinding(style = Style.RPC)
- @SuppressWarnings("deprecation")
- public class TestService implements IService {
- public User getUserByName(@WebParam(name = "name") String name) {
- User user = new User();
- user.setId(new Date().getSeconds());
- user.setName(name);
- user.setAddress("china");
- user.setEmail(name + "@test.com");
- return user;
- }
- public void setUser(User user) {
- System.out.println("############Server setUser###########");
- System.out.println("setUser:" + user);
- }
- }
2.7 在src目录下建立applicationContext-client.xml,并放入以下内容:
- <span xmlns="http://www.w3.org/1999/xhtml" style=""><span xmlns="http://www.w3.org/1999/xhtml" style=""><?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:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <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"/>
- <bean id="userServiceBean" class="com.use.test.ws.server.TestService"/>
- <bean id="inMessageInterceptor" class="org.apache.cxf.interceptor.LoggingInInterceptor"/>
- <bean id="outLoggingInterceptor" class="org.apache.cxf.interceptor.LoggingOutInterceptor"/>
- <jaxws:server id="userService" serviceClass="com.use.test.ws.server.IService" address="/Users">
- <jaxws:serviceBean>
- <ref bean="userServiceBean"/>
- </jaxws:serviceBean>
- <jaxws:inInterceptors>
- <ref bean="inMessageInterceptor"/>
- </jaxws:inInterceptors>
- <jaxws:outInterceptors>
- <ref bean="outLoggingInterceptor"/>
- </jaxws:outInterceptors>
- </jaxws:server>
- </beans>
- </span></span><span style="color:#099000;"><span style="color:#000000;font-weight: bold;"><span style="color:#000000;font-weight: bold;"></span></span></span>
2.8 将建立的Java工程导出jar包,只导出src下面的内容即,并将这个jar包放到上面的TOMCAT工程中的ws_test/WEB-INF/lib目录下,并启动TOMCAT服务器;
2.9 启动成功后,此时访问URL:http://localhost:8080/ws_test/Users?wsdl,应该会看到WSDL的输出。
3 编写客户端
3.1 独立的客户端请求类
- package com.use.test.ws.client;
- import org.apache.cxf.jaxws.JaxWsProxyFactoryBean;
- import com.use.test.ws.entity.User;
- import com.use.test.ws.server.IService;
- public class TestClient {
- public static void main(String[] args) {
- JaxWsProxyFactoryBean factory = new JaxWsProxyFactoryBean();
- factory.setServiceClass(IService.class);
- factory.setAddress("http://localhost:8080/ws_test/Users");
- IService service = (IService) factory.create();
- System.out.println("#############Client getUserByName##############");
- User user = service.getUserByName("hoojo");
- System.out.println(user);
- user.setAddress("China-Guangzhou");
- service.setUser(user);
- }
- }
点击右键就可以运行这个类。
3.2 与SPRING的集成,首先需要在src下面建立配置文件applicationContext-client.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:xsi="http://www.w3.org/2001/XMLSchema-instance"
- xsi:schemaLocation="http://www.springframework.org/schema/beans
- http://www.springframework.org/schema/beans/spring-beans-3.0.xsd
- http://www.springframework.org/schema/context
- http://www.springframework.org/schema/context/spring-context-3.0.xsd
- http://cxf.apache.org/jaxws
- http://cxf.apache.org/schemas/jaxws.xsd">
- <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:client id="userWsClient" serviceClass="com.use.test.ws.server.IService" address="http://localhost:8080/ws_test/Users"/>
- </beans>
3.3 建立JAVA类
- package com.use.test.ws.client;
- import org.springframework.context.ApplicationContext;
- import org.springframework.context.support.ClassPathXmlApplicationContext;
- import com.use.test.ws.entity.User;
- import com.use.test.ws.server.IService;
- /**
- * <b>function:</b>请求Spring整合CXF的WebService客户端
- */
- public class SpringUsersWsClient {
- public static void main(String[] args) {
- ApplicationContext ctx = new ClassPathXmlApplicationContext("applicationContext-client.xml");
- IService service = ctx.getBean("userWsClient", IService.class);
- System.out.println("#############Client getUserByName##############");
- User user = service.getUserByName("hoojo");
- System.out.println(user);
- user.setAddress("China-Guangzhou");
- service.setUser(user);
- }
- }
直接运行这个类就OK了。
同事可以参考github上的代码:https://github.com/zhangyuchenoo/webservice
以上是关于一个CXF集成SPRING的WEBSERVICE完整实例的主要内容,如果未能解决你的问题,请参考以下文章