Spring Boot / Jersey组件扫描问题

时间:2017-02-02 03:40:08

标签: rest spring-boot jersey

我正在使用Spring Boot和Jersey作为一个小应用程序,我需要添加@ComponentScan来扫描第三方库以获取某些服务。问题是,每当我添加@ComponentScan注释时,似乎Spring MVC DispatcherServlet接管了,我再也无法点击我的Jersey端点(当我添加注释时,它看起来不像JerseyServlet甚至被加载)。如何让组件扫描与我的Jersey端点一起工作?

的build.gradle

    buildscript {
    ext {
        springBootVersion = '1.5.1.RELEASE'
    }
    repositories {
        mavenCentral()
    }
    dependencies {
        classpath("org.springframework.boot:spring-boot-gradle-plugin:${springBootVersion}")
    }
  }

apply plugin: 'java'
apply plugin: 'eclipse'
apply plugin: 'org.springframework.boot'

jar {
    baseName = 'demo'
    version = '0.0.1-SNAPSHOT'
}

sourceCompatibility = 1.8

repositories {
    mavenCentral()
}


dependencies {
    compile('org.springframework.boot:spring-boot-starter-jersey')
    compile('org.springframework.boot:spring-boot-starter-web')
    testCompile('org.springframework.boot:spring-boot-starter-test')
}

应用

@SpringBootApplication
@ComponentScan(basePackages = {"com.abc"})
public class DemoApplication {
    public static void main(String[] args) {
        SpringApplication.run(DemoApplication.class, args);
    }
}

配置

@Component
@Configuration
public class JerseyConfig extends ResourceConfig {
    @PostConstruct
    public void init() {
        packages("com.demo.rest.endpoint");

        register(new LoggingFeature(Logger.getLogger(LoggingFeature.DEFAULT_LOGGER_NAME), Level.INFO, LoggingFeature.DEFAULT_VERBOSITY, null));
    }
}

端点

@Component
@Path("/test")
public class TestEndpoint {
    @GET
    public String test() {
        return "Testing Jersey Endpoint";
    }
}

1 个答案:

答案 0 :(得分:0)

这是球衣如何在Fatjars上扫描类的问题。保留所有内容并在JerseyConfig上注册每个类。

@Component
@Configuration
public class JerseyConfig extends ResourceConfig 
{

     @PostConstruct
     public void init() {
     [...]

     register(TestEndpoint.class);
}