本教程向您展示如何将Jersey Web应用程序与Spring框架集成。
使用的技术:
- 球衣1.8
- Spring 3.0.5。发布
- Eclipse 3.6
- Maven 3
1.项目依赖
在Maven pom.xml
文件中声明Jersey 1.8,Spring3和“ jersey-spring.jar ”依赖项。
注意
在“ jersey-spring.jar ”版本中,它将下载所有Spring 2.5.6依赖项。 要使用Spring 3,您需要手动排除那些旧的Spring库。
<repositories>
<repository>
<id>maven2-repository.java.net</id>
<name>Java.net Repository for Maven</name>
<url>http://download.java.net/maven/2/</url>
</repository>
</repositories>
<dependencies>
<!-- Jersey -->
<dependency>
<groupId>com.sun.jersey</groupId>
<artifactId>jersey-server</artifactId>
<version>1.8</version>
</dependency>
<!-- Spring 3 dependencies -->
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
<version>3.0.5.RELEASE</version>
</dependency>
<!-- Jersey + Spring -->
<dependency>
<groupId>com.sun.jersey.contribs</groupId>
<artifactId>jersey-spring</artifactId>
<version>1.8</version>
<exclusions>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-core</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-web</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-beans</artifactId>
</exclusion>
<exclusion>
<groupId>org.springframework</groupId>
<artifactId>spring-context</artifactId>
</exclusion>
</exclusions>
</dependency>
</dependencies>
2.Spring Bean
在Spring Ioc容器中注册了一个简单的“ transactionBo” bean。 稍后,您将把这个bean注入Jersey服务。
package com.mkyong.transaction;
public interface TransactionBo{
String save();
}
package com.mkyong.transaction.impl;
import com.mkyong.transaction.TransactionBo;
public class TransactionBoImpl implements TransactionBo {
public String save() {
return "Jersey + Spring example";
}
}
File:applicationContext.xml –注册Bean并启用组件自动扫描功能。
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns:context="http://www.springframework.org/schema/context"
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">
<context:component-scan base-package="com.mkyong.rest" />
<bean id="transactionBo"
class="com.mkyong.transaction.impl.TransactionBoImpl" />
</beans>
3.球衣
在REST方法中,您可以将Spring中的“ transactionBo” bean自动注入到Jersey中。
package com.mkyong.rest;
import javax.ws.rs.GET;
import javax.ws.rs.Path;
import javax.ws.rs.core.Response;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;
import com.mkyong.transaction.TransactionBo;
@Component
@Path("/payment")
public class PaymentService {
@Autowired
TransactionBo transactionBo;
@GET
@Path("/mkyong")
public Response savePayment() {
String result = transactionBo.save();
return Response.status(200).entity(result).build();
}
}
4.将Jersey与Spring集成
核心集成在web.xml
- 注册Spring“
ContextLoaderListener
”侦听器类 - 将Jersey servlet从“
com.sun.jersey.spi.container.servlet.ServletContainer
”更改为“com.sun.jersey.spi.spring.container.servlet.SpringServlet
”。
档案:web.xml
<web-app id="WebApp_ID" version="2.4"
xmlns="http://java.sun.com/xml/ns/j2ee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee
http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd">
<display-name>Restful Web Application</display-name>
<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>
<servlet>
<servlet-name>jersey-serlvet</servlet-name>
<servlet-class>
com.sun.jersey.spi.spring.container.servlet.SpringServlet
</servlet-class>
<init-param>
<param-name>
com.sun.jersey.config.property.packages
</param-name>
<param-value>com.mkyong.rest</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>jersey-serlvet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
</web-app>
5.演示
下载源代码
下载它– Jersey-Spring-Integration-Example.zip (8 KB)
参考文献
翻译自: https://mkyong.com/webservices/jax-rs/jersey-spring-integration-example/