将Jersey 2和Spring与基于Java的配置集成

我正在使用Jersey 2.10和jersey-spring3以及Spring 4.我想在泽西资源以及其他地方实现DI(基本上是服务)并且想要通过Java配置创建Spring Beans。

目前,我无法找到任何方法来做到这一点。 知道怎么做吗?

我的web.xml看起来像这样

 Restful Web Application  jersey-serlvet  org.glassfish.jersey.servlet.ServletContainer    jersey.config.server.provider.packages  com.xyz  1   contextConfigLocation /WEB-INF/application-context.xml   org.springframework.web.context.ContextLoaderListener   jersey-serlvet /*   

web应用程序:

  contextClass  org.springframework.web.context.support.AnnotationConfigWebApplicationContext    contextConfigLocation xxx.xxx.configuration.ApplicationConfiguration   org.springframework.web.context.ContextLoaderListener   SpringApplication org.glassfish.jersey.servlet.ServletContainer  jersey.config.server.provider.classnames xxx.xxx.controllers.HelloController  1   SpringApplication /*  

基于Java的配置:

 @Configuration public class ApplicationConfiguration { @Bean HelloService helloService () { return new HelloServiceImpl(); } } 

和简单的控制器:

 @Component @Path("/helloController") public class HelloController { @Autowired @Qualifier("helloService") private HelloService helloService ; @GET @Path("/hello") public String hello() { helloService.service(); } } 

用于检测:

本地主机:8080 / [AppName的] / HelloController中/你好

记住如果不这样做,可以排除旧的Spring依赖项。 您可以像下面的示例或通过DependencyManagement一样执行此操作。

    org.glassfish.jersey.ext jersey-spring3 2.11   spring-context org.springframework   spring-beans org.springframework   spring-core org.springframework   spring-web org.springframework   jersey-server org.glassfish.jersey.core    jersey-container-servlet-core  org.glassfish.jersey.containers   hk2 org.glassfish.hk2      org.springframework spring-core 4.0.6.RELEASE   org.springframework spring-context 4.0.6.RELEASE   org.springframework spring-beans 4.0.6.RELEASE   org.springframework spring-web 4.0.6.RELEASE   org.springframework spring-aspects 4.0.6.RELEASE   

老式的方式:

由于您已经初始化了ContextLoaderListener一个简单的技巧是使用WebApplicationContext在任何应用程序点检索您的bean:

 WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext); SomeBean someBean = (SomeBean) ctx.getBean("someBean"); 

泽西支持:

或者您可以使用基于注释的发现,因为Jersey已经支持Spring DI 。 您必须在主应用程序入口点注册bean。 下面的示例中的入口点是some.package.MyApplication ,应该作为servlet容器的提供:

  SpringApplication org.glassfish.jersey.servlet.ServletContainer  javax.ws.rs.Application some.package.MyApplication  1  

在您的应用程序中注册bean:

 package some.package; import org.glassfish.jersey.server.ResourceConfig; import org.glassfish.jersey.server.spring.scope.RequestContextFilter; public class MyApplication extends ResourceConfig { public MyApplication () { register(RequestContextFilter.class); register(SomeBean.class); // ... } } 

在这里,您可以查看Jersey Git repo的准备好运行示例。

这是我从各种教程开始发现的东西。 结合其他答案,你应该有一个完整的例子。

 import javax.servlet.ServletContext; import javax.servlet.ServletException; import javax.servlet.ServletRegistration; import com.sun.jersey.spi.spring.container.servlet.SpringServlet; import org.springframework.web.WebApplicationInitializer; import org.springframework.web.context.WebApplicationContext; import org.springframework.web.context.support.AnnotationConfigWebApplicationContext; public class WebInitializer implements WebApplicationInitializer { @Override public void onStartup(ServletContext servletContext) throws ServletException { AnnotationConfigWebApplicationContext ctx = new AnnotationConfigWebApplicationContext(); ctx.register(AppConfig.class); ctx.setServletContext(servletContext); servletContext.setAttribute(WebApplicationContext.ROOT_WEB_APPLICATION_CONTEXT_ATTRIBUTE, ctx); ServletRegistration.Dynamic servlet = servletContext.addServlet("jersey-serlvet", new SpringServlet()); servlet.addMapping("/"); servlet.setLoadOnStartup(1); } } 

对于那些尝试使用Java配置的人:

  public static void main(String[] args) throws IOException { HttpServer server = new HttpServer(); NetworkListener listener = new NetworkListener("grizzly2", "localhost", 2088); server.addListener(listener); WebappContext ctx = new WebappContext("ctx","/"); final ServletRegistration reg = ctx.addServlet("spring", new SpringServlet()); reg.addMapping("/*"); ctx.addContextInitParameter( "contextClass", "org.springframework.web.context.support.AnnotationConfigWebApplicationContext" ); ctx.addContextInitParameter( "contextConfigLocation", "com.example.AppConfig" ); ctx.addListener( "org.springframework.web.context.ContextLoaderListener" ); ctx.addListener("org.springframework.web.context.request.RequestContextListener"); ctx.deploy(server); server.start(); System.in.read(); }