当前位置: 首页 > 面试题库 >

结合Spring项目和Jersey

通寂离
2023-03-14
问题内容

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

问题答案:

你无需在需要的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>


 类似资料:
  • 问题内容: 我正在使用Maven开发Swing独立应用程序。我尝试遵循MVC模式。我对我的项目结构感到困惑。我有这样的事情: 现在,我想合并Spring框架,这使我可以放置DAO和BO接口和实现。我已阅读本文链接,建议的项目结构不适合我的项目。我想到的是添加以下内容: dao目录的内容如下所示(在模型目录中包含Client和Customer类): 这不好吗?我想学习好的做法。 问题答案: 您可以遵

  • 我有一个测试Web服务项目,我在其中使用REST(泽西)JSON Spring。问题是我不知道如何将它们组合在一起。例如,我有一个have类,它可以与数据库一起使用: 我还有一个JSON类,它获取并返回JSON中的对象: 现在我试着把它们结合起来。我想从DB中获取数据,并将其转换为JSON,然后在响应中返回: 当我用最后一个组合开始项目时,我得到了这个错误: 我的pom.xml: My web.x

  • 实际上,我是使用Maven和spring的新手,我很好奇我们是如何标准化项目结构的。正如我们所知,如果我们第一次创建Maven项目,它只给出了这样的结构: 我明白,Java目录是我们放Java代码的目录。但是,当我们制作一个应该有HTML、CSS或Javascript等web文件的web应用程序时,我们需要另一个目录来放置它。 当我在Maven中使用spring或spring boot时,它也在主

  • 当默认的项目结构不适用时,可以自定义配置。查看 Gradle 文档中 Java plugin 部分以了解如何在纯 Java 项目中进行配置。 Android plugin 使用了类似的语法,但因为 Android 有自己的 sourceSets,所以需要配置到 android 块中。下面的例子使用了旧的项目结构(Eclipse),并把 androidTest 的 sourceSet 映射到 tes

  • 问题内容: 我想从dnsmasq收集和处理日志,因此决定使用ELK。Dnsmasq用作DHCP服务器和DNS解析器,因此它为这两种服务创建日志条目。 我的目标是将所有包含请求者IP,请求者主机名(如果有)和请求者mac地址的DNS查询发送到Elasticsearch。这样一来,无论设备IP是否更改,我都可以按mac地址对请求进行分组,并显示主机名。 我想做的是以下几点: 1)阅读以下条目: 2)临