我的應用程式中有兩個檔案。
主類檔案。
package in.njari.util;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
@SpringBootApplication
@ComponentScan(basePackages = "in.njari")
public class UtilApplication {
public static void main(String[] args) {
SpringApplication.run(UtilApplication.class, args);
}
}
控制器類。
package in.njari.util.src.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class UtilController {
@GetMapping(value = "/util")
public String f(){
return "I provide insights.";
}
}
我的 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 錯誤。
因此,即使控制器被拾取進行組件掃描(根據日志),其中的請求也不會被映射。不知道為什么會這樣。
我懷疑我在這里使用/未使用的配置/注釋有誤!雖然無法確定它到底是什么。
uj5u.com熱心網友回復:
嘗試洗掉
@ComponentScan(basePackages = "in.njari")
注釋@RestController是系統將自己識別和初始化的java bean,其他然后你的代碼似乎作業我已經測驗過了。

uj5u.com熱心網友回復:
從 Spring Boot 2.1.x 開始,“spring.mvc.servlet.path”屬性被移動并重命名為“spring.mvc.servlet.path”。
你能試一下嗎
spring.mvc.servlet.path=/util-service
代替
server.servlet.context-path=/util-service
uj5u.com熱心網友回復:
如果您明確要掃描基本包,請使用
@SpringBootApplication(scanBasePackages = "in.njari")
轉載請註明出處,本文鏈接:https://www.uj5u.com/qukuanlian/457328.html
標籤:爪哇 弹簧靴 雄猫 http-status-code-404
