JAX-RS 和 Spring 整合开发
步骤
- 1.建立maven web工程
- 2.建立坐标,在pom.xml中导入需要依赖的jar包
完整的pom.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.0http://maven.apache.org/xsd/maven-4.0.0.xsd";>
;
<modelVersion>4.0.0</modelVersion>
<groupId>cn.itcast.maven</groupId>
<artifactId>cxf_rs_spring</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>
<name>cxf_rs_spring</name>
<dependencies>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-frontend-jaxrs</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-log4j12</artifactId>
<version>1.7.12</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-client</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.apache.cxf</groupId>
<artifactId>cxf-rt-rs-extension-providers</artifactId>
<version>3.0.1</version>
</dependency>
<dependency>
<groupId>org.codehaus.jettison</groupId>
<artifactId>jettison</artifactId>
<version>1.3.7</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>4.1.7.RELEASE</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9800</port>
</configuration>
</plugin>
</plugins>
</build>
</project>
- 3.配置web.xml
完整的web.xml如下:
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="
http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="
http://java.sun.com/xml/ns/javaeehttp://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
id="WebApp_ID" version="2.5">
<!-- spring配置文件位置 -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:applicationContext.xml</param-value>
</context-param>
<!-- spring核心监听器 -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<servlet>
<servlet-name>CXFService</servlet-name>
<servlet-class>org.apache.cxf.transport.servlet.CXFServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>CXFService</servlet-name>
<url-pattern>/
services
/*</url-pattern>
</servlet-mapping>
<welcome-file-list>
<welcome-file>index.html</welcome-file>
<welcome-file>index.htm</welcome-file>
<welcome-file>index.jsp</welcome-file>
<welcome-file>default.html</welcome-file>
<welcome-file>default.htm</welcome-file>
<welcome-file>default.jsp</welcome-file>
</welcome-file-list>
</web-app>
- 4.导入实体类和service
- 5.、 在 spring 配置发布 rs 服务
引入名称空间
xmlns:jaxrs="
http://cxf.apache.org/jaxrs"
http://cxf.apache.org/jaxrshttp://cxf.apache.org/schemas/jaxrs.xsd
配置服务:
完整的applicationContext.xml:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
http://www.springframework.org/schema/beans"
xsi:schemaLocation="
<jaxrs:server id="xxxService" address="/xxxService" >
<jaxrs:serviceBeans>
<bean class="cn.itcast.cxf.service.xxxServiceImpl" />
</jaxrs:serviceBeans>
<jaxrs:inInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingInInterceptor" />
</jaxrs:inInterceptors>
<jaxrs:outInterceptors>
<bean class="org.apache.cxf.interceptor.LoggingOutInterceptor" />
</jaxrs:outInterceptors>
</jaxrs:server>
</beans>
.配置服务启动端口(如果上面的pom.xml中没有配置,才需要配置这个,上面已经有了完整了pom.xml)
<build>
<plugins>
<plugin>
<groupId>org.codehaus.mojo</groupId>
<artifactId>tomcat-maven-plugin</artifactId>
<version>1.1</version>
<configuration>
<port>9800</port>
</configuration>
</plugin>
</plugins>
</build>
- 6.编写客户端代码 类似独立服务客户端代码 WebClient 工具实现
1) .在src/test/resource下面创建一个applicationContext-test.xml
内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="
http://www.springframework.org/schema/beans"
xsi:schemaLocation="
</beans>
2).创建client类
package cn.itcast.cxf.client;
import java.util.Collection;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import org.apache.cxf.jaxrs.client.WebClient;
import cn.itcast.cxf.domain.User;
/**
* @author: yangzhaoli
* @creationTime:2017年11月13日 下午6:44:34
* @version: 1.1
*/
public class Cxf_rs_springClient {
public static void main(String[] args) {
/*//查询根据id
WebClient client2 = WebClient.create("
http://localhost:9800/cxf_rs_spring/services/userService/user/1"
);
User user2 = client2.accept(MediaType.APPLICATION_JSON).
get
(User.class);
System.out.println(user2);*/
//查询所有
/*WebClient webClient = WebClient.create("
http://localhost:9800/cxf_rs_spring/services/userService/user").accept(MediaType.APPLICATION_XML
);
Collection<? extends User> collection = webClient.
getCollection
(User.class);
System.out.println(collection);*/
//增加
/*User user = new User();
user.setId(9999);
user.setUsername("haha");
WebClient webClient = WebClient.create("
http://localhost:9800/cxf_rs_spring/services/userService/user"
);
webClient.type(MediaType.APPLICATION_JSON).
post
(user);*/
//修改
WebClient client = WebClient.create("
http://localhost:9800/cxf_rs_spring/services/userService/user"
);
User user = new User();
user.setId(2);
user.setUsername("hhh");
client.
put
(user);
}
}
注意:最终访问资源服务路径: 服务器根目录地址 + web.xml 配置(url ) + applicationContext.xml address 配置 + 类 @Path + 方法 @Path