После поиска различных похожих вопросов в StackOverflow я определил, что большинство людей, страдающих от этой проблемы, не сканируют должным образом модуль, в котором живет их контроллер.Некоторые решения требуют объединения файлов, которые должны быть отсканированы, в тот же модуль, что и приложение (которое работает), но я не хочу перемещать ни один из моих файлов .java.Вместо этого я хотел бы заставить @ComponentScan
работать.
У меня есть следующая структура проекта
Project
|
settings.gradle
build.gradle
module1
|
src/main/java/com.test.application
| |
| Application.java
| SwaggerConfig.java
build.gradle
module2
|
src/main/java/com.test.service
| |
| Service.java
| Controller.java
build.gradle
Application.java
@SpringBootApplication
@ComponentScan("com.test")
public class Application {
public static void main(String[] args) {
SpringApplication.run(Application.class, args);
}
}
Обратите внимание, что я сканирую com.test
в моем компонентном сканировании.В документации @ ComponentScan (или в StackOverflow) не найдено ничего, что указывало бы на неправильное использование @ComponentScan
.
Controller.java
@RestController
@RequestMapping("/test")
public class GearParsingController {
private SomeService someService;
@Autowired
public SomeController(SomeService someService) {
this.someService = someService;
}
@GetMapping("/path")
public ResponseEntity<String> getSomeService() {
return new ResponseEntity<String>("Default message", HttpStatus.OK);
}
}
Проект build.gradle
buildscript {
repositories {
mavenCentral()
}
dependencies {
classpath("org.springframework.boot:spring-boot-gradle-plugin:2.1.6.RELEASE")
}
}
subprojects {
repositories {
mavenCentral()
}
apply plugin: 'java'
apply plugin: 'idea'
apply plugin: 'org.springframework.boot'
apply plugin: 'io.spring.dependency-management'
}
module1 build.gradle
dependencies {
implementation project(':module2')
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.6.RELEASE'
}
module2 build.gradle
dependencies {
implementation group: 'org.springframework.boot', name: 'spring-boot-starter-web', version: '2.1.6.RELEASE'
testImplementation group: 'org.junit.jupiter', name: 'junit-jupiter-api', version: '5.5.0-RC2'
}
Когда я пытаюсь получить доступ к своим конечным точкам в localhost:8080/test/path
, меня встречают:
{
"timestamp": "2019-06-29T19:19:52.275+0000",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/test/path"
}
При запуске программы в режиме отладки мне даютследующий вывод:
2019-06-29 13:58:58.345 INFO 8272 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2019-06-29 13:58:58.387 INFO 8272 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2019-06-29 13:58:58.387 INFO 8272 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.21]
2019-06-29 13:58:58.522 INFO 8272 --- [ main] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext
2019-06-29 13:58:58.522 DEBUG 8272 --- [ main] o.s.web.context.ContextLoader : Published root WebApplicationContext as ServletContext attribute with name [org.springframework.web.context.WebApplicationContext.ROOT]
2019-06-29 13:58:58.522 INFO 8272 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 1786 ms
2019-06-29 13:58:58.544 DEBUG 8272 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping filters: characterEncodingFilter urls=[/*], hiddenHttpMethodFilter urls=[/*], formContentFilter urls=[/*], requestContextFilter urls=[/*]
2019-06-29 13:58:58.545 DEBUG 8272 --- [ main] o.s.b.w.s.ServletContextInitializerBeans : Mapping servlets: dispatcherServlet urls=[/]
2019-06-29 13:58:58.583 DEBUG 8272 --- [ main] o.s.b.w.s.f.OrderedRequestContextFilter : Filter 'requestContextFilter' configured for use
2019-06-29 13:58:58.583 DEBUG 8272 --- [ main] .s.b.w.s.f.OrderedHiddenHttpMethodFilter : Filter 'hiddenHttpMethodFilter' configured for use
2019-06-29 13:58:58.583 DEBUG 8272 --- [ main] s.b.w.s.f.OrderedCharacterEncodingFilter : Filter 'characterEncodingFilter' configured for use
2019-06-29 13:58:58.584 DEBUG 8272 --- [ main] o.s.b.w.s.f.OrderedFormContentFilter : Filter 'formContentFilter' configured for use
2019-06-29 13:58:58.896 DEBUG 8272 --- [ main] s.w.s.m.m.a.RequestMappingHandlerMapping : 5 mappings in 'requestMappingHandlerMapping'
2019-06-29 13:58:58.990 INFO 8272 --- [ main] pertySourcedRequestMappingHandlerMapping : Mapped URL path [/v2/api-docs] onto method [public org.springframework.http.ResponseEntity<springfox.documentation.spring.web.json.Json> springfox.documentation.swagger2.web.Swagger2Controller.getDocumentation(java.lang.String,javax.servlet.http.HttpServletRequest)]
2019-06-29 13:58:59.045 DEBUG 8272 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/**/favicon.ico] in 'faviconHandlerMapping'
2019-06-29 13:58:59.128 INFO 8272 --- [ main] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor'
2019-06-29 13:58:59.146 DEBUG 8272 --- [ main] s.w.s.m.m.a.RequestMappingHandlerAdapter : ControllerAdvice beans: 0 @ModelAttribute, 0 @InitBinder, 1 RequestBodyAdvice, 1 ResponseBodyAdvice
2019-06-29 13:58:59.257 DEBUG 8272 --- [ main] o.s.w.s.handler.SimpleUrlHandlerMapping : Patterns [/webjars/**, /**] in 'resourceHandlerMapping'
2019-06-29 13:58:59.263 DEBUG 8272 --- [ main] .m.m.a.ExceptionHandlerExceptionResolver : ControllerAdvice beans: 0 @ExceptionHandler, 1 ResponseBodyAdvice
2019-06-29 13:58:59.442 INFO 8272 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Context refreshed
2019-06-29 13:58:59.481 INFO 8272 --- [ main] d.s.w.p.DocumentationPluginsBootstrapper : Found 1 custom documentation plugin(s)
2019-06-29 13:58:59.519 INFO 8272 --- [ main] s.d.s.w.s.ApiListingReferenceScanner : Scanning for api listing references
Итак, мой вопрос: Почему ComponentScan не обнаруживает мой контроллер REST?