spring-boot - 带有Jersey的Spring Boot 2.1.0

标签 spring-boot jersey

今天,我启动了一个简单的应用程序spring boot应用程序。因为我从头开始,所以我使用的是SpringBoot的最新版本:2.1.0.RELEASE

我想使用Jersey来使用JAX-RS。我的版本适用于1.3.6 Spring Boot版本,但出现以下错误:

***************************
APPLICATION FAILED TO START
***************************

Description:

The bean 'requestContextFilter', defined in class path resource [org/springframework/boot/autoconfigure/web/servlet/WebMvcAutoConfiguration$WebMvcAutoConfigurationAdapter.class], could not be registered. A bean with that name has already been defined in class path resource [org/springframework/boot/autoconfigure/jersey/JerseyAutoConfiguration.class] and overriding is disabled.

Action:

Consider renaming one of the beans or enabling overriding by setting spring.main.allow-bean-definition-overriding=true

我不明白问题可能出在哪里,因为此时我的应用程序极简。

显然,bean'requestContextFilter'被配置了两次,但是我不知道它在哪里配置。

这是我的配置:

pom.xml
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <start-class>pt.msoftware.userauthservice.App</start-class>
    <java.version>1.8</java.version>
    <docker.image.prefix>${user.name}</docker.image.prefix>
</properties>

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>

    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-actuator</artifactId>

    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>

        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-jersey</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-devtools</artifactId>
        <optional>true</optional>
    </dependency>
</dependencies>

SpringBoot应用程序类
@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}

** Jersey 配置**
import org.glassfish.jersey.server.ResourceConfig;
import org.springframework.stereotype.Component;
import pt.msoftware.userauthservice.rest.UserEndpoint;

import javax.ws.rs.ApplicationPath;

/**
 * Created by marco on 31/10/2018.
 */
@Component
@ApplicationPath("/rest")
public class JerseyConfig extends ResourceConfig {

    public JerseyConfig() {
        register(UserEndpoint.class);
    }

}

**端点**
import org.springframework.stereotype.Component;

import javax.ws.rs.GET;
import javax.ws.rs.Path;

/**
 * Created by marco on 31/10/2018.
 */
@Component
@Path("/user")
public class UserEndpoint {
    @GET
    public String message() {
        return "Hello";
    }
}

有人可以发现我所缺少的内容或我的代码/配置有什么问题吗?

非常感谢

最佳答案

这是Spring Boot中的错误。感谢您引起我们的注意。我已经打开this issue来跟踪问题。

如果只打算使用Jersey和JAX-RS,则不需要使用spring-boot-starter-web。本质上,它是基于Spring MVC的spring-boot-starter-jersey的等效项。因此,可以通过从应用程序中删除spring-boot-starter-web依赖项来避免遇到的问题。

如果确实要同时使用Spring MVC和JAX-RS,则可以通过将spring.main.allow-bean-definition-overriding=true添加到application.properties中的src/main/resources文件中来启用bean定义覆盖。

关于spring-boot - 带有Jersey的Spring Boot 2.1.0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53091306/

相关文章:

Spring boot @Decimalmin 和 @Decimalmax 不在请求中工作

java - 在 TestNG 测试中获取 org.hibernate.LazyInitializationException

java - com.din.OSS 中构造函数的参数 0 需要类型为 'java.lang.String' 的 bean,但无法找到

java - 将数据添加到 Jersey 中的 Response 对象

android - Jersey 客户端需要 Android 中缺少的 XMLInputFactory

java - 将 Jersey 1.8 更新到 1.10+ 会导致 ClassNotFoundException : com. sun.jersey.server.impl.cdi.AnnotatedCallableImpl

spring - 在 Spring Data JPA 中以 3 种不同的方式创建查询

java - 如何在 Spring Boot 中使用缓存来提高 OAuth2 ResourceServer 的性能

java - Jersey,JAXB 并获取一个扩展抽象类的对象作为参数

java - 如何检索 Jersey ContainerRequest 属性?