Jersey支持spring3配置

本文介绍了如何将Jersey RESTful服务与Spring3框架进行整合,通过web.xml配置和使用Spring的@Component注解在Jersey资源类中注入依赖。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1. web.xml配置

<!-- Spring config start -->
	<context-param>
		<param-name>contextConfigLocation</param-name>
		<param-value>classpath*:spring-*.xml</param-value>
	</context-param>
	<listener>
		<listener-class>org.springframework.web.context.ContextLoaderListener
		</listener-class>
	</listener>

	<!-- jersey -->
	<servlet>
		<servlet-name>Jersey Web Application</servlet-name>
		<servlet-class>org.glassfish.jersey.servlet.ServletContainer
		</servlet-class>
		<init-param>
			<param-name>jersey.config.server.provider.packages</param-name>
			<param-value>com.lianlian.box.web.resource</param-value>
		</init-param>
		<load-on-startup>1</load-on-startup>
	</servlet>
	<servlet-mapping>
		<servlet-name>Jersey Web Application</servlet-name>
		<url-pattern>/webapi/*</url-pattern>
	</servlet-mapping>

2.spring的配置就不详说了


3. jersey的Resource,这里的@Component注解是spring的

@Component
@Path("/tp/ad")
public class TpAdRequestResource {
	
	@Resource
	private AdService adService;

	@Path("/apply")
	@Produces(MediaType.APPLICATION_JSON)
	public AdApplyRespParam adApply(@BeanParam AdApplyReqParam param) {
		adService.sendAdApplyRequest(param);
		return null;
	}
	
}

4. 只加注解还不够,一定要引入jersey-spring3的Jar包。百度了好长时间,都没get到重点,google第一篇就找到了,原文地址:https://jersey.java.net/documentation/latest/spring.html

		<dependency>
			<groupId>org.glassfish.jersey.ext</groupId>
			<artifactId>jersey-spring3</artifactId>
			<version>2.23</version>
		</dependency>


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值