Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/340.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 结合Spring项目和Jersey_Java_Spring_Jersey_Jax Rs - Fatal编程技术网

Java 结合Spring项目和Jersey

Java 结合Spring项目和Jersey,java,spring,jersey,jax-rs,Java,Spring,Jersey,Jax Rs,我已经用Spring JPA构建了一个项目,现在我想在我的Jersey项目中使用它。 我已经将我的SJPA项目作为依赖项添加到pom.xml中 当我使用GET/POST/PUT/DELETE方法时,我想使用我的SJPA中的服务类。 有没有一种简单的方法可以通过注释实现这一点?或者我必须在每个类中获取注释ConfigApplicationContext?感觉有点浪费 @Path("/users") @Produces(MediaType.APPLICATION_JSON) @Consumes(Me

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

当我使用GET/POST/PUT/DELETE方法时,我想使用我的SJPA中的服务类。 有没有一种简单的方法可以通过注释实现这一点?或者我必须在每个类中获取
注释ConfigApplicationContext
?感觉有点浪费

@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();
    }
}

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


您不需要在需要服务的地方创建
ApplicationContext
。您应该能够配置一个全局的。Jersey为此提供了一个模块,该模块集成了两个框架。这允许您只需将所有Spring服务
@自动连接到Jersey资源类中

我不会试图给出任何例子,我只会链接到官方的例子。他们是直接从项目,所以链接应该是好的一段时间。请特别注意Maven依赖项。您将需要确保让它们在示例中发挥作用

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

[1]-关于Java配置示例,需要注意的一点是它使用一个独立的应用程序。在webapp中使用Java配置需要一些技巧。Jersey在此处查找带有
applicationContext.xml
文件位置的param
contextConfigLocation
,如果找不到,将抛出异常

我找到了一些解决这个问题的方法

  • 提出这一问题的人提到了一个例子。您可以创建一个SpringWeb初始值设定项,在其中配置Spring上下文并重写param属性。(参见完整示例)

  • 您只需将
    applicationContext.xml
    添加到类路径,并将SpringJava配置类注册为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>
    

    非常感谢您提供了一个描述良好的答案!我已经检查了您的示例,并使用了带有Java配置的示例,包括“bug fix”。但我确实遇到了一个wierd异常“未能读取候选组件类…ASM ClassReader未能解析类文件-可能是因为一个新的Java类文件版本还不受支持”将尝试使用XML,所以暂时确定,所以我进行了一些搜索,看起来像是。依赖项使用的是Spring3,但Java8似乎需要Spring4。因此,如果您想使用Java8,就像我说的,您应该从
    jersey-spring3
    依赖项中排除所有可传递的依赖项,并将Spring版本更改为4.x verison。我将发布一个例子,我想补充一件事!版本4.1.0.RELEASE出现问题,导致“org.springframework.beans.BeanUtils.findPropertyForMethod”我所做的只是使用更新到4.1.5.RELEASE,在这里我找到了答案“…但您需要确保排除所有Spring可传递依赖项”这本可以挽救我昨天5小时的生命…完全忽略了这个暗示。谢谢
    <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>
    
    <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>