带Jersey的Spring Boot 2.1.0

mps*_*tos 9 jersey spring-boot

今天我开始了一个简单的应用程序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
Run Code Online (Sandbox Code Playgroud)

我无法理解问题出在哪里,因为我此时的应用程序极简主义.

显然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>
Run Code Online (Sandbox Code Playgroud)

SpringBoot应用程序类

@SpringBootApplication
public class App {
    public static void main(String[] args) {
        SpringApplication.run(App.class, args);
    }
}
Run Code Online (Sandbox Code Playgroud)

**泽西配置**

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

}
Run Code Online (Sandbox Code Playgroud)

**端点**

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";
    }
}
Run Code Online (Sandbox Code Playgroud)

有人可以发现我丢失的内容或我的代码/配置可能出错吗?

非常感谢

And*_*son 13

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

如果您打算只使用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=trueapplication.properties文件来启用bean定义覆盖src/main/resources.