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>