结合 Spring 项目和 Jersey

Combining Spring project and Jersey

我已经使用 Spring JPA 构建了一个项目,现在我想在我的 Jersey 项目中使用它。 我已将我的 SJPA 项目添加为 pom.xml

中的依赖项

我想在使用 GET/POST/PUT/DELETE 方法时使用我的 SJPA 中的服务 classes。 有没有一种简单的方法可以使用注释来做到这一点?或者我是否必须在每个 class 中得到 AnnotationConfigApplicationContext?感觉有点浪费。

@Path("/users")
@Produces(MediaType.APPLICATION_JSON)
@Consumes(MediaType.APPLICATION_JSON)
public final class UserResource
{
    private AnnotationConfigApplicationContext context = new AnnotationConfigApplicationContext();
    private PodcastService service;

    @GET
    public Response getAllPodcasts() {
        context.scan("org.villy.spring.service");
        context.refresh();
        service= context.getBean(PodcastService.class);
        return Response.ok(service.findAll()).build();
    }
}

注意:下面的 linked 示例项目来自 Jersey master 分支,目前是 Jersey 3 的快照,尚未发布。 Jersey 3 将使用 Spring 4,因此您可能会注意到一个依赖关系 jersey-spring4。这种依赖性还不存在,因为 Jersey 3 还没有发布(可能暂时不会)。所以要使用的依赖是jersey-spring3。所有的例子应该仍然是一样的,只是改变了那个依赖。如果要使用 Spring 4,请参阅此答案下面示例 pom 中列出的依赖项


您不需要在需要服务的地方创建 ApplicationContext。您应该能够配置一个全局的。 Jersey 有一个集成这两个框架的模块。这允许您简单地 @Autowired 所有 Spring 服务到您的 Jersey 资源 classes.

我不会尝试生成任何示例,而只是 link 官方示例。它们直接来自项目,因此 links 应该可以使用一段时间。特别注意 Maven 依赖项。您需要确保拥有它们才能使示例正常工作。

注:示例中的${spring3.version}版本为3.2.3.RELEASE。可以在示例中使用 Spring 4,但您需要确保从 jersey-spring3 依赖项中排除所有 Spring 传递依赖项。

[1] - 关于 Java 配置示例需要注意的一件事是它使用独立应用程序。要在 Web 应用程序中使用 Java 配置需要一些技巧。这是 a known bug,Jersey 在其中查找具有 applicationContext.xml 文件位置的参数 contextConfigLocation,当找不到时将抛出异常。

我找到了一些解决方法。

  1. 提出问题的人提到了一个例子。您可以创建一个 Spring Web 初始值设定项,您可以在其中配置 spring 上下文 覆盖参数 属性。 (参见完整示例 here)。

    @Order(1)
    public class SpringWebContainerInitializer implements WebApplicationInitializer {
    
        @Override
        public void onStartup(ServletContext servletContext) throws ServletException {
            registerContextLoaderListener(servletContext);
    
            // Set the Jersey used property to it won't load a ContextLoaderListener
            servletContext.setInitParameter("contextConfigLocation", "");
        }
    
        private void registerContextLoaderListener(ServletContext servletContext) {
            WebApplicationContext webContext;
            webContext = createWebAplicationContext(SpringAnnotationConfig.class);
            servletContext.addListener(new ContextLoaderListener(webContext));
        }
    
        public WebApplicationContext createWebAplicationContext(Class... configClasses) {
            AnnotationConfigWebApplicationContext context;
            context = new AnnotationConfigWebApplicationContext();
            context.register(configClasses);
            return context;
        }
    }
    
  2. 您可以简单地将 applicationContext.xml 添加到 class 路径,并将 spring Java 配置 class 注册为豆子

    <beans xmlns="http://www.springframework.org/schema/beans"
           xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
           xmlns:context="http://www.springframework.org/schema/context"
           xsi:schemaLocation="http://www.springframework.org/schema/beans
           http://www.springframework.org/schema/beans/spring-beans.xsd
           http://www.springframework.org/schema/context
           http://www.springframework.org/schema/context/spring-context.xsd">
    
    
        <context:annotation-config/>
        <bean id="config" class="com.your.pkg.SpringAnnotationConfig"/>
    
    </beans>
    
  3. 还有一个方法我能想到,但是我把它保存了一段时间,我可以实际测试一下。


更新

"Failed to read candidate component class ... ASM ClassReader failed to parse class file - probably due to a new Java class file version that isn't supported yet"

似乎与this有关,使用Spring 3和Java 8。就像我说的,如果你想使用Spring 4,你将需要从 jersey-spring3 中排除 Spring 传递依赖项,只需更改明确声明的 Spring 依赖项的版本。这是一个例子,我测试过并且有效。

<dependency>
    <groupId>org.glassfish.jersey.ext</groupId>
    <artifactId>jersey-spring3</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-beans</artifactId> 
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-core</artifactId> 
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-aop</artifactId> 
        </exclusion>
        <exclusion>
            <groupId>org.springframework</groupId>
            <artifactId>spring-web</artifactId> 
        </exclusion>
    </exclusions>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>4.1.0.RELEASE</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-aop</artifactId>
    <version>4.1.0.RELEASE</version>
    <scope>compile</scope>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjrt</artifactId>
    <version>1.8.1</version>
</dependency>
<dependency>
    <groupId>org.aspectj</groupId>
    <artifactId>aspectjweaver</artifactId>
    <version>1.8.1</version>
</dependency>