将 Jersey 2 和 Spring 与基于 Java 的配置集成 | 珊瑚贝

Integrating Jersey 2 and Spring with Java Based Configuration


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

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

我的 web.xml 看起来像这样

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
<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>

  • 除了部署描述符之外,到目前为止,您还尝试过什么让一些 bean 由容器管理?有什么努力吗?
  • 我知道 annotationconfigapplicationcontext 用于加载 @Configurable 类来创建/管理 beans。但不知道如何使用 Jersey 来实现这一点。


网络应用程序:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
<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>

基于 Java 的配置:

1
2
3
4
5
6
7
@Configuration
public class ApplicationConfiguration {
  @Bean
  HelloService helloService () {
    return new HelloServiceImpl();
  }
}

和简单的控制器:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
@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 依赖项,否则可能会产生一些冲突。您可以按照下面的示例或通过 DependencyManagement 执行此操作。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
<dependencies>

    <!– Jersey –>

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

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

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

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

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

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

</dependencies>

  • 你能分享这个代码吗?例如,例如。 :)
  • 奇迹般有效。谢谢你。
  • AFAIK 仅当您需要需要代理的 Spring 功能(例如 @Transactional)时才需要 @Component 注释。
  • 为了使用 jersey 和 spring,我们必须创建什么样的项目?我尝试创建一个动态 Web 项目,但没有得到任何 pom.xml 文件。我们必须在哪个文件中添加上述依赖项和排除项?
  • 在 Eclipse 中,我使用插件:M2E – Eclipse 的 Maven 集成。然后您可以右键单击您的项目 -> 配置 -> 转换为 Maven 项目。然后你会看到 pom.xml 文件。


老套路:

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

1
2
WebApplicationContext ctx = WebApplicationContextUtils.getRequiredWebApplicationContext(servletContext);
SomeBean someBean = (SomeBean) ctx.getBean(“someBean”);

泽西支持:

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

1
2
3
4
5
6
7
8
9
<servlet>
  <servlet–name>SpringApplication</servlet–name>
  <servlet–class>org.glassfish.jersey.servlet.ServletContainer</servlet–class>
  <init–param>
    <param–name>javax.ws.rs.Application</param–name>
    <param–value>some.package.MyApplication</param–value>
  </init–param>
  <load–on–startup>1</load–on–startup>
</servlet>

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

1
2
3
4
5
6
7
8
9
10
11
12
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 中的准备运行示例。

  • 1.如何使用您的旧方法。我将在哪个类中创建上下文。我的意思是泽西岛正在扫描带注释的类以注册资源。如何使用上下文组合类。?
  • 2.为什么我应该使用@Component.3.Your链接指向使用applicationContext(beans are created in applicationContext)4.我可以在MyApplication中注册@Configurable Class(包含我的beans)班级。
  • 在这里,您一次接触了太多 Spring 面孔,我绝对不会问这些,因为它超出了主要问题,因为我认为我的回答非常清楚,甚至提供了两种实现 OP 目标的方法。比说我可以做这个或那个更好的是自己尝试并探索结果,如果您在某些时候感到困惑或困惑,请回到这里,如果我收到您的帖子,我将很高兴为您提供支持 :)
  • 我非常感谢您的回答。我会尝试您的建议,如果可行,我会接受。


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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
    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();

}


这是我从各种教程中发现的。结合其他答案,您应该有一个完整的示例。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
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);
    }
}



来源:https://www.codenong.com/25701658/

微信公众号
手机浏览(小程序)
0
分享到:
没有账号? 忘记密码?