【问题标题】:Combining Spring project and Jersey结合 Spring 项目和 Jersey
【发布时间】:2015-09-02 14:25:46
【问题描述】:

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

当我使用 GET/POST/PUT/DELETE 方法时,我想使用我的 SJPA 中的服务类。 有没有一种简单的方法可以通过注释来做到这一点?或者我必须在每个班级获得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();
    }
}

【问题讨论】:

    标签: java spring jersey jax-rs


    【解决方案1】:

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


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

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

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

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

    我找到了一些解决方法。

    1. 提出问题的人提到了一个例子。您可以创建一个 Spring Web 初始化程序,在其中配置 Spring 上下文 覆盖 param 属性。 (参见完整示例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 添加到类路径中,并将 Spring Java 配置类注册为 bean

      <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. 我还能想到另一种方法,但我已经把它留了一段时间,我可以实际测试一下。


    更新

    “未能读取候选组件类...ASM ClassReader 无法解析类文件 - 可能是由于尚不支持的新 Java 类文件版本”

    似乎与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>
    

    【讨论】:

    • 非常感谢您提供详细描述的答案!我已经检查了您的示例并使用了带有 Java 配置的示例,包括“错误修复”。但我确实遇到了一个奇怪的异常“无法读取候选组件类...... ASM ClassReader 无法解析类文件 - 可能是由于尚不支持的新 Java 类文件版本” 可能会尝试使用 XML 所以现在
    • 好的,所以我做了一些搜索,它看起来像this is the problem。依赖项使用 Spring 3,但 Java 8 似乎需要 Spring 4。因此,如果您想使用 Java 8,就像我说的那样,您应该从 jersey-spring3 依赖项中排除所有传递依赖项,并将 Spring 版本更改为4.x 版本。我将发布一个示例,
    • 我想再补充一点!版本 4.1.0.RELEASE 出现问题导致:“org.springframework.beans.BeanUtils.findPropertyForMethod” 我所做的只是使用更新到 4.1.5.RELEASE ,在那里我找到了here的答案
    • "...但是您需要确保排除所有 Spring 传递依赖" 这本来可以节省我昨天 5 小时的生命...完全忽略了那个提示。谢谢!
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2010-10-25
    • 2011-12-20
    • 1970-01-01
    • 2013-09-22
    • 1970-01-01
    相关资源
    最近更新 更多