Spring 启动 - 控制器被识别但 api 未绑定
Spring Boot - Controller is recognised but the api is not binded
我的申请中有两个文件。
主 class 文件。
package in.njari.util;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UtilApplication {
public static void main(String[] args) {
SpringApplication.run(UtilApplication.class, args);
}
}
控制器class.
package in.njari.util.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UtilController {
@GetMapping("/util")
public ResponseEntity<?> f(){
return new ResponseEntity<>("I provide insights.", HttpStatus.OK);
}
}
我的 application.properties 看起来像:
server.servlet.context-path=/util-service
logging.level.org.springframework.core.io.support=DEBUG
logging.level.org.springframework.context.annotation=DEBUG
这是我的控制台的样子:
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2022-04-06 11:19:53.819 INFO 57700 --- [ main] in.njari.util.UtilApplication : Starting UtilApplication on njari-MacBook-Pro.local with PID 57700 (/Users/rajdeep/X/util/target/classes started by rajdeep in /Users/rajdeep/X/util)
2022-04-06 11:19:53.821 INFO 57700 --- [ main] in.njari.util.UtilApplication : No active profile set, falling back to default profiles: default
2022-04-06 11:19:54.005 DEBUG 57700 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/Users/njari/X/util/target/classes/in/njari/util/src/controller/UtilController.class]
2022-04-06 11:19:54.615 INFO 57700 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-06 11:19:54.627 INFO 57700 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-06 11:19:54.627 INFO 57700 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2022-04-06 11:19:54.708 INFO 57700 --- [ main] o.a.c.c.C.[.[localhost].[/util-service] : Initializing Spring embedded WebApplicationContext
2022-04-06 11:19:54.709 INFO 57700 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 830 ms
2022-04-06 11:19:54.861 INFO 57700 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/util-service'
2022-04-06 11:19:54.866 INFO 57700 --- [ main] in.njari.util.UtilApplication : Started UtilApplication in 6.463 seconds (JVM running for 6.993)
我尝试用 url 击中 api :
http://localhost:8080/util-service/util
这会引发 404 错误。
因此,即使控制器被拾取用于组件扫描(根据日志),其中的请求也未映射。不确定为什么会这样。
我怀疑我在这里配置请求的方式有问题!虽然无法确定它到底是什么。
尝试移除
@ComponentScan(basePackages = "in.njari")
注释 @RestController
是一个 java bean,系统会自己识别和初始化,除此之外你的代码似乎可以工作我已经测试过了。
如果您明确要扫描基本包,请使用
@SpringBootApplication(scanBasePackages = "in.njari")
编辑:更新答案w.r.t。你的代码
将 spring-web 依赖项更改为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
我的申请中有两个文件。
主 class 文件。
package in.njari.util;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
@SpringBootApplication
public class UtilApplication {
public static void main(String[] args) {
SpringApplication.run(UtilApplication.class, args);
}
}
控制器class.
package in.njari.util.controller;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UtilController {
@GetMapping("/util")
public ResponseEntity<?> f(){
return new ResponseEntity<>("I provide insights.", HttpStatus.OK);
}
}
我的 application.properties 看起来像:
server.servlet.context-path=/util-service
logging.level.org.springframework.core.io.support=DEBUG
logging.level.org.springframework.context.annotation=DEBUG
这是我的控制台的样子:
/\ / ___'_ __ _ _(_)_ __ __ _ \ \ \ \
( ( )\___ | '_ | '_| | '_ \/ _` | \ \ \ \
\/ ___)| |_)| | | | | || (_| | ) ) ) )
' |____| .__|_| |_|_| |_\__, | / / / /
=========|_|==============|___/=/_/_/_/
:: Spring Boot :: (v2.2.5.RELEASE)
2022-04-06 11:19:53.819 INFO 57700 --- [ main] in.njari.util.UtilApplication : Starting UtilApplication on njari-MacBook-Pro.local with PID 57700 (/Users/rajdeep/X/util/target/classes started by rajdeep in /Users/rajdeep/X/util)
2022-04-06 11:19:53.821 INFO 57700 --- [ main] in.njari.util.UtilApplication : No active profile set, falling back to default profiles: default
2022-04-06 11:19:54.005 DEBUG 57700 --- [ main] o.s.c.a.ClassPathBeanDefinitionScanner : Identified candidate component class: file [/Users/njari/X/util/target/classes/in/njari/util/src/controller/UtilController.class]
2022-04-06 11:19:54.615 INFO 57700 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http)
2022-04-06 11:19:54.627 INFO 57700 --- [ main] o.apache.catalina.core.StandardService : Starting service [Tomcat]
2022-04-06 11:19:54.627 INFO 57700 --- [ main] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.31]
2022-04-06 11:19:54.708 INFO 57700 --- [ main] o.a.c.c.C.[.[localhost].[/util-service] : Initializing Spring embedded WebApplicationContext
2022-04-06 11:19:54.709 INFO 57700 --- [ main] o.s.web.context.ContextLoader : Root WebApplicationContext: initialization completed in 830 ms
2022-04-06 11:19:54.861 INFO 57700 --- [ main] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '/util-service'
2022-04-06 11:19:54.866 INFO 57700 --- [ main] in.njari.util.UtilApplication : Started UtilApplication in 6.463 seconds (JVM running for 6.993)
我尝试用 url 击中 api :
http://localhost:8080/util-service/util
这会引发 404 错误。
因此,即使控制器被拾取用于组件扫描(根据日志),其中的请求也未映射。不确定为什么会这样。
我怀疑我在这里配置请求的方式有问题!虽然无法确定它到底是什么。
尝试移除
@ComponentScan(basePackages = "in.njari")
注释 @RestController
是一个 java bean,系统会自己识别和初始化,除此之外你的代码似乎可以工作我已经测试过了。
如果您明确要扫描基本包,请使用
@SpringBootApplication(scanBasePackages = "in.njari")
编辑:更新答案w.r.t。你的代码
将 spring-web 依赖项更改为
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>