小编典典

结合Spring项目和Jersey

spring

我已经用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();
    }
}

阅读 528

收藏
2020-04-11

共1个答案

小编典典

你无需在需要的ApplicationContext位置创建服务。你应该能够配置一个全局的。为此,Jersey提供了一个模块,该模块集成了两个框架。这使你可以将@Autowired所有Spring服务简单地放入Jersey资源类中。

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

[ 1 ]-关于Java配置示例的一件事要注意的是它使用一个独立的应用程序。要在Web应用程序中使用Java配置,需要一些技巧。这是一个已知的错误,Jersey会在该错误中查找contextConfigLocation具有applicationContext.xml文件位置的参数,并且在找不到该参数时会引发异常。

我发现了一些解决方法。

  1. 提出问题的人提到了一个这样的例子。你可以创建一个Spring Web初始化程序,在其中配置Spring上下文并覆盖param属性。
@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;
    }
}
  1. 你可以简单地将其添加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>
  1. 我可以想到另一种方法,但是我已经保存了一段时间,可以进行实际测试了。

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

似乎与此有关,将Spring 3与Java 8一起使用。就像我说的那样,如果要使用Spring 4,则需要从中排除Spring传递依赖项,jersey-spring3而只更改显式声明的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>
2020-04-11