Rest Spring引导/Jersey组件扫描问题
我正在使用Spring Boot和Jersey开发一个小应用程序,我需要添加@ComponentScan来扫描第三方库中的一些服务。问题是,每当我添加@ComponentScan注释时,似乎Spring MVC DispatcherServlet就接管了,我再也不能访问Jersey端点(添加注释时,JerseyServlet看起来甚至都没有加载)。如何使组件扫描与Jersey端点一起工作 build.gradleRest Spring引导/Jersey组件扫描问题,rest,spring-boot,jersey,Rest,Spring Boot,Jersey,我正在使用Spring Boot和Jersey开发一个小应用程序,我需要添加@ComponentScan来扫描第三方库中的一些服务。问题是,每当我添加@ComponentScan注释时,似乎Spring MVC DispatcherServlet就接管了,我再也不能访问Jersey端点(添加注释时,JerseyServlet看起来甚至都没有加载)。如何使组件扫描与Jersey端点一起工作 build.gradle buildscript { ext { sprin
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";
}
}
jersey如何扫描Fatjar上的类是个问题。保留所有内容并在您的运动衫配置中添加注册每个类
@Component
@Configuration
public class JerseyConfig extends ResourceConfig
{
@PostConstruct
public void init() {
[...]
register(TestEndpoint.class);
}
@SpringBootApplication
已经定义了一个直接递归扫描当前数据的@ComponentScan
。不确定,但您的@ComponentScan
可能会覆盖该选项。尝试定义一个@Configuration
类,并将@ComponentScan
放在该类上。是的,这解决了问题。非常感谢!!