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

将Jersey 2和Spring与基于Java的配置集成,java,spring,rest,jersey-2.0,Java,Spring,Rest,Jersey 2.0,我正在使用Jersey 2.10、Jersey-spring3和Spring 4。 我希望在jersey资源和其他地方实现DI(基本上是服务),并希望通过Java配置创建Springbean 目前,我无法找到任何方法来做到这一点。 你知道怎么做吗 我的web.xml如下所示 <web-app> <display-name>Restful Web Application</display-name> <servlet>

我正在使用Jersey 2.10、Jersey-spring3和Spring 4。 我希望在jersey资源和其他地方实现DI(基本上是服务),并希望通过Java配置创建Springbean

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

我的web.xml如下所示

<web-app>
    <display-name>Restful Web Application</display-name>
    <servlet>
        <servlet-name>jersey-serlvet</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.xyz</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/application-context.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet-mapping>
        <servlet-name>jersey-serlvet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

Restful Web应用程序
泽西塞尔维特酒店
org.glassfish.jersey.servlet.ServletContainer
jersey.config.server.provider.packages
com.xyz
1.
上下文配置位置
/WEB-INF/application-context.xml
org.springframework.web.context.ContextLoaderListener
泽西塞尔维特酒店
/*
老式方式: 由于您已经初始化了
ContextLoaderListener
,一个简单的技巧是使用
WebApplicationContext
在任何应用程序点检索bean:

WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
SomeBean someBean = (SomeBean) ctx.getBean("someBean");
泽西岛支持: 或者,您也可以使用基于注释的发现,因为。您必须在主应用程序入口点下注册bean。在下面的示例中,该入口点将是
some.package.MyApplication
,应该作为servlet容器的
提供:


SpringApplication
org.glassfish.jersey.servlet.ServletContainer
javax.ws.rs.Application
some.package.MyApplication
1.
在应用程序中注册Bean:

package some.package;
导入org.glassfish.jersey.server.ResourceConfig;
导入org.glassfish.jersey.server.spring.scope.RequestContextFilter;
公共类MyApplication扩展了ResourceConfig{
公共应用程序(){
寄存器(RequestContextFilter.class);
寄存器(SomeBean.class);
// ...
}
}
您可以从Jersey Git repo查看一个随时可用的示例。

web应用程序:

<context-param>
    <param-name>contextClass</param-name>
    <param-value>
      org.springframework.web.context.support.AnnotationConfigWebApplicationContext
  </param-value>
</context-param>

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>xxx.xxx.configuration.ApplicationConfiguration</param-value>
</context-param>

<listener>
    <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>

<servlet>
    <servlet-name>SpringApplication</servlet-name>
    <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
    <init-param>
        <param-name>jersey.config.server.provider.classnames</param-name>
        <param-value>xxx.xxx.controllers.HelloController</param-value>
    </init-param>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>SpringApplication</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
和简单控制器:

@Component
@Path("/helloController")
public class HelloController {

  @Autowired
  @Qualifier("helloService")
  private HelloService helloService ;


   @GET
   @Path("/hello")
   public String hello() {
    helloService.service();
  }
}
对于测试:

localhost:8080/[AppName]/helloController/hello

记住,如果不排除旧的Spring依赖项,可能会有一些冲突。您可以像下面的示例一样,或通过DependencyManager来执行此操作

<dependencies>

    <!-- Jersey -->

    <dependency>
        <groupId>org.glassfish.jersey.ext</groupId>
        <artifactId>jersey-spring3</artifactId>
        <version>2.11</version>
        <exclusions>
            <exclusion>
                <artifactId>spring-context</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-beans</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-core</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>spring-web</artifactId>
                <groupId>org.springframework</groupId>
            </exclusion>
            <exclusion>
                <artifactId>jersey-server</artifactId>
                <groupId>org.glassfish.jersey.core</groupId>
            </exclusion>
            <exclusion>
                <artifactId>
                    jersey-container-servlet-core
                </artifactId>
                <groupId>org.glassfish.jersey.containers</groupId>
            </exclusion>
            <exclusion>
                <artifactId>hk2</artifactId>
                <groupId>org.glassfish.hk2</groupId>
            </exclusion>
        </exclusions>
    </dependency>

    <!-- Spring 4 dependencies -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-core</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-beans</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-web</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-aspects</artifactId>
        <version>4.0.6.RELEASE</version>
    </dependency>

</dependencies>

org.glassfish.jersey.ext
泽西-斯普林3
2.11
spring上下文
org.springframework
春豆
org.springframework
弹簧芯
org.springframework
弹簧网
org.springframework
泽西服务器
org.glassfish.jersey.core
jersey容器servlet核心
org.glassfish.jersey.containers
hk2
org.glassfish.hk2
org.springframework
弹簧芯
4.0.6.1发布
org.springframework
spring上下文
4.0.6.1发布
org.springframework
春豆
4.0.6.1发布
org.springframework
弹簧网
4.0.6.1发布
org.springframework
春季方面
4.0.6.1发布

以下是我从各种教程中找到的内容。结合其他答案,你应该有一个完整的例子

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 config执行此操作的用户:

    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();

}

除了部署描述符,到目前为止,您尝试了哪些方法来获得容器管理的bean?任何努力?我知道annotationconfigapplicationcontext用于加载@Configurable类以创建/管理bean。但不知道如何使用Jersey实现这一点。1.如何使用旧方法。我将在哪个类中创建上下文。我的意思是Jersey正在扫描注释类以注册资源。如何使用上下文类。?2.我为什么要使用@Component.3使我的jersey资源成为Spring托管资源。您的链接指向使用applicationContext(bean是在applicationContext中创建的)4.我可以注册@Configurable类(其中包含我的bean)在我的应用程序类中,这里你在一次拍摄中接触了太多的Spring面,我绝对不会问这些问题,因为这与主要问题无关,因为我认为我的答案非常清楚,甚至提供了两种实现OP目标的方法。比说“我能做这个或那个”更好的是自己尝试并探索结果,如果你在某一点上感到困惑或困惑,请回到这里,我个人会很高兴支持你,如果我看到你的帖子:)我真的很感谢你的回答。我会尝试你的建议,然后如果它有效,我接受它。你能分享这段代码吗?例如,例如:)工作起来很有魅力。谢谢。如果您需要需要代理的Spring功能,例如
@Transactional
,则只需要
@组件
注释。为了使用jersey和Spring,我们必须创建什么样的项目?我尝试创建一个动态web项目,但没有得到任何pom.xml文件。我们必须在哪个文件中添加上述依赖项和排除项?在Eclipse中,我使用插件:“M2E-Maven Integration for Eclipse”。然后,您可以右键单击项目->配置->转换为Maven项目。然后您将看到pom.xml文件。
    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();

}