Mavne + Spring整合CXF

来源:互联网 发布:mvc数据传到前台 编辑:程序博客网 时间:2024/06/16 05:35

例子运行环境:maven+spring+cxf+tomcat


服务端代码:

一、编辑pom.xml文件,引入所需jar文件:

<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/maven-v4_0_0.xsd"><modelVersion>4.0.0</modelVersion><groupId>com.darren.testing.apache.cxf</groupId><artifactId>HelloWorld</artifactId><packaging>war</packaging><version>0.0.1-SNAPSHOT</version><name>HelloWorld Maven Webapp</name><url>http://maven.apache.org</url><build><plugins><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-compiler-plugin</artifactId><configuration><source>1.7</source><target>1.7</target><encoding>UTF-8</encoding></configuration></plugin><plugin><groupId>org.apache.maven.plugins</groupId><artifactId>maven-surefire-plugin</artifactId><configuration><!-- 跳过测试单元 --><skip>true</skip></configuration></plugin></plugins></build><properties><junit.version>4.11</junit.version><cxf.version>2.2.3</cxf.version><spring.version>3.2.3.RELEASE</spring.version><slf4j.version>1.7.7</slf4j.version></properties><dependencies><dependency><groupId>junit</groupId><artifactId>junit</artifactId><version>${junit.version}</version></dependency><!-- CXF Dependencies --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-frontend-jaxws</artifactId><version>${cxf.version}</version></dependency><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http</artifactId><version>${cxf.version}</version></dependency><!-- Jetty is needed if you're are not using the CXFServlet --><dependency><groupId>org.apache.cxf</groupId><artifactId>cxf-rt-transports-http-jetty</artifactId><version>${cxf.version}</version></dependency><!-- End of CXF Dependencies --><!-- Spring Dependencies ${spring.version} --><dependency><groupId>org.springframework</groupId><artifactId>spring-context</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.springframework</groupId><artifactId>spring-web</artifactId><version>${spring.version}</version></dependency><dependency><groupId>org.slf4j</groupId><artifactId>slf4j-log4j12</artifactId><version>${slf4j.version}</version><type>jar</type><scope>compile</scope></dependency></dependencies></project>


二、创建实体类:User

package com.darren.service.facade.domain;import java.io.Serializable;public class User implements Serializable{/** *  */private static final long serialVersionUID = 1L;private int id;private String name = null;private String address = null;private String email = null;public User(){}public User(int id, String name, String address, String email) {this.id = id;this.name = name;this.address = address;this.email = email;}public int getId() {return id;}public void setId(int id) {this.id = id;}public String getName() {return name;}public void setName(String name) {this.name = name;}public String getAddress() {return address;}public void setAddress(String address) {this.address = address;}public String getEmail() {return email;}public void setEmail(String email) {this.email = email;}@Overridepublic String toString() {return "id:" + this.id + ", name:" + this.name;}}

三、创建要发布的接口:UserServiceFacade

package com.darren.service.facade;import javax.jws.WebParam;import javax.jws.WebService;import com.darren.service.facade.domain.User;@WebServicepublic interface UserServiceFacade {public User getUserByName(@WebParam(name ="name") String name);public void setUser(User user);}
说明:

1、@WebService:将该接口发布成WebService服务器

2、@WebParam,指定参数的名称,默认是args0,args1,args2......


四、创建实现UserServiceFacade接口的Class:UserServiceImpl

package com.darren.service;import java.util.HashMap;import java.util.Map;import javax.jws.WebService;import com.darren.service.facade.UserServiceFacade;import com.darren.service.facade.domain.User;@WebServicepublic class UserServiceImpl implements UserServiceFacade {private static Map<String,User> users = null;private static void init(){users = new HashMap<String,User>();users.put("张三", new User(1,"张三","上海浦东","zs@163.com"));users.put("李斯", new User(2,"李斯","上海浦东","ls@163.com"));users.put("王五", new User(3,"王五","上海浦东","ww@163.com"));}@Overridepublic User getUserByName(String name) {if(users == null){init();}return users.get(name);}@Overridepublic void setUser(User user) {// TODO Auto-generated method stub}}

五、配置applicationContext.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" /><!-- 要暴露给外部调用的接口,address:请求路径 --><jaxws:endpoint  implementor="com.darren.service.UserServiceImpl" address="/user"></jaxws:endpoint></beans>

六、配置web.xml

<?xml version="1.0" encoding="UTF-8"?><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_3_0.xsd"version="3.0"><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.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>/services/*</url-pattern></servlet-mapping></web-app>

至此CXF服务端的代码已全部写完;


客户端代码:

一、编写Client测试类:

package com.darren.service.facade;import org.springframework.context.ApplicationContext;import org.springframework.context.support.ClassPathXmlApplicationContext;import com.darren.service.facade.domain.User;public class UserServiceFacadeTest {public static void main(String[] args){//加载Spring配置文件ApplicationContext context = new ClassPathXmlApplicationContext("applicationContext-client.xml");//从Spring容器中获取对象UserServiceFacade userService = context.getBean("userWsClient",UserServiceFacade.class);//远程调用接口User u = userService.getUserByName("张三");if(u != null){System.out.println(u);}}}

二、配置客户端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" /><!-- serviceClass:要访问的接口,address:接口地址 --><jaxws:client id="userWsClient" serviceClass="com.darren.service.facade.UserServiceFacade"address="http://localhost:8080/HelloWorld/services/user?wsdl" /></beans>

说明:address="http://localhost:8080/HelloWorld/services/user?wsdl"

            http://localhost:8080/HelloWorld:工程访问路径,

            /services:此路径在web.xml里面配置的

            /user:此路径在服务端的applicationContext.xml里配置的


三、发布项目到Tomcat,并启动Tomcat


四、运行UserServiceFacadeTest类,运行结果如下图所示:



最后附上完整的项目结果图:






0 0