1.引言
在目前的JavaWeb开发过程中,Spring有着至关重要的地位,所以也就存在着需求:CXF与Spring的整合,CXF和Spring是可以无缝整合的,在本篇博客中就是要用maven实现spring与CXF的整合。我们最终要完成的项目目录如下图所示:
所实现的功能有:
- 将Person,PersonService,PersonServiceImpl发布为Rest类型的服务
- 将User ,UserService,UserServiceImpl发布为Soap类型的服务
- 允许我们的Web服务可以跨域请求
2.项目实现
2.1POM.xml文件
<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>spring</groupId>
<artifactId>cxf</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<name>cxf Maven Webapp</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<cxf.version>3.1.9</cxf.version>
<jackson.version>1.9.13</jackson.version>
<spring.version>4.1.9.RELEASE</spring.version>
</properties>
<dependencies>
<!-- Spring -->
<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>
<!-- CXF -->
<!--cxf-rt-frontend-jaxws支持soap类型服务-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxws</artifactId>
<version>${cxf.version}</version>
</dependency>
<!--cxf-rt-frontend-jaxrs支持rest服务-->
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>${cxf.version}</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-transports-http</artifactId>
<version>${cxf.version}</version>
</dependency>
<!--用于跨域的jar包-->
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>java-property-utils</artifactId>
<version>1.9</version>
</dependency>
<dependency>
<groupId>com.thetransactioncompany</groupId>
<artifactId>cors-filter</artifactId>
<version>1.7</version>
</dependency>
<!-- 用于序列化json服务 -->
<dependency>
<groupId>org.codehaus.jackson</groupId>
<artifactId>jackson-jaxrs</artifactId>
<version>${jackson.version}</version>
</dependency>
<!--测试框架-->
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.9</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<finalName>cxf</finalName>
</build>
</project>
2.2web.xml文件
<!DOCTYPE web-app PUBLIC
"-//Sun Microsystems, Inc.//DTD Web Application 2.3//EN"
"http://java.sun.com/dtd/web-app_2_3.dtd" >
<web-app>
<display-name>Archetype Created Web Application</display-name>
<!-- 通过上下文参数指定spring配置文件的位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:spring.xml</param-value>
</context-param>
<!--用于设置跨域的拦截器-->
<filter>
<filter-name>CORS</filter-name>
<filter-class>com.thetransactioncompany.cors.CORSFilter</filter-class>
<!--允许哪一些客户端跨域-->
<init-param>
<param-name>cors.allowOrigin</param-name>
<param-value>*</param-value>
</init-param>
<init-param>
<param-name>cors.supportedMethods</param-name>
<param-value>GET, POST, HEAD, PUT, DELETE</param-value>
</init-param>
<init-param>
<param-name>cors.supportedHeaders</param-name>
<param-value>Accept, Origin, X-Requested-With, Content-Type, Last-Modified</param-value>
</init-param>
<init-param>
<param-name>cors.exposedHeaders</param-name>
<param-value>Set-Cookie</param-value>
</init-param>
<init-param>
<param-name>cors.supportsCredentials</param-name>
<param-value>true</param-value>
</init-param>
</filter>
<!-- 编码过滤器 -->
<filter>
<filter-name>encodingFilter</filter-name>
<filter-class>org.springframework.web.filter.CharacterEncodingFilter</filter-class>
<init-param>
<param-name>encoding</param-name>
<param-value>UTF-8</param-value>
</init-param>
</filter>
<filter-mapping>
<filter-name>encodingFilter</filter-name>
<url-pattern>/*</url-pattern>
</filter-mapping>
<filter-mapping>
<filter-name>CORS</filter-name>
<!--允许跨域的路径,这里只允许我们发布的Web服务跨域-->
<url-pattern>/ws/*</url-pattern>
</filter-mapping>
<!-- 配置spring提供的上下文载入器监听器,当项目启动时加载spring配置文件,初始化spring工厂 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>cxf</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
</servlet>
<servlet-mapping>
<servlet-name>cxf</servlet-name>
<url-pattern>/ws/*</url-pattern>
</servlet-mapping>
<!--springmvc的配置-->
</web-app>
2.3soap接口,实现类,实体类
- User实体类
package com.skd.domain;
public class User {
private String username;
private String password;
public String getUsername() {
return username;
}
public void setUsername(String username) {
this.username = username;
}
public String getPassword() {
return password;
}
public void setPassword(String password) {
this.password = password;
}
}
- UserService接口
package com.skd.service;
import com.skd.domain.User;
import javax.jws.WebService;
@WebService
public interface UserService {
public User getUser(String name);
}
- 接口实现类
package com.skd.service.impl;
import com.skd.domain.User;
import com.skd.service.UserService;
public class UserServiceImpl implements UserService{
public User getUser(String name) {
User u=new User();
u.setUsername(name);
u.setPassword("123456");
return u;
}
}
2.4rest接口,实现类,实体类
- 实体类
package com.skd.domain;
import java.io.Serializable;
public class Person implements Serializable{
private String name;
private int age;
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public int getAge() {
return age;
}
public void setAge(int age) {
this.age = age;
}
}
- 接口
package com.skd.service;
import com.skd.domain.Person;
import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;
@Produces( {MediaType.APPLICATION_JSON })
public interface PersonService {
@POST
@Path("/person/{name}")
public Person getPerson(@PathParam("name") String name);
@POST
@Path("/persons")
public List<Person> getPersons();
}
- 接口实现类
package com.skd.service.impl;
import com.skd.domain.Person;
import com.skd.service.PersonService;
import java.util.ArrayList;
import java.util.List;
public class PersonServiceImpl implements PersonService{
public Person getPerson(String name) {
Person p=new Person();
p.setName(name);
p.setAge(100);
return p;
}
public List<Person> getPersons() {
List<Person> persons=new ArrayList<Person>();
Person p1=new Person();
p1.setName("wpx");
p1.setAge(100);
persons.add(p1);
Person p2=new Person();
p2.setName("zlr");
p2.setAge(100);
persons.add(p2);
return persons;
}
}
2.5spring和cxf配置文件
- CXF.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"
xmlns:jaxrs="http://cxf.apache.org/jaxrs"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
http://cxf.apache.org/jaxws
http://cxf.apache.org/schemas/jaxws.xsd
http://cxf.apache.org/jaxrs
http://cxf.apache.org/schemas/jaxrs.xsd">
<!--
id:当前服务的唯一标识 address:当前服务对应的请求url serviceClass:接口类型
-->
<jaxws:server id="soapuser" address="/user"
serviceClass="com.skd.service.UserService">
<jaxws:serviceBean>
<bean class="com.skd.service.impl.UserServiceImpl" />
</jaxws:serviceBean>
</jaxws:server>
<!-- 发布一个Restful方式的WebService服务 -->
<bean id="personServiceImpl" class="com.skd.service.impl.PersonServiceImpl"></bean>
<jaxrs:server id="personService" address="/rest">
<jaxrs:serviceBeans>
<ref bean="personServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:providers>
<!--提供序列化为json数据的实现类-->
<bean class="org.codehaus.jackson.jaxrs.JacksonJsonProvider"></bean>
</jaxrs:providers>
<jaxrs:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor"></bean>
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor"></bean>
</jaxrs:outInterceptors>
</jaxrs:server>
</beans>
- spring
<?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:p="http://www.springframework.org/schema/p"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="http://www.springframework.org/schema/beans
http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context
http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc
http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
<import resource="cxf.xml"></import>
</beans>
2.6验证服务是否发布成功
访问地址http://localhost:8080/ws查看到下图便发布成功:
3.代码下载
CXF和Spring整合代码下载地址为:CXF和Spring整合