我創建了這個簡單的 springboot 應用程式:
SpringbootBackendApplication.java
package com.example.demo;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.context.annotation.ComponentScan;
import controller.CaseFileController;
@SpringBootApplication
@ComponentScan(basePackageClasses = CaseFileController.class)
public class SpringbootBackendApplication {
public static void main(String[] args) {
SpringApplication.run(SpringbootBackendApplication.class, args);
}
}
和
案例檔案控制器.java
package controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api/v1/")
public class CaseFileController {
@GetMapping("/hello")
public String helloWorld() {
return "Hello World";
}
}
但是,當我嘗試訪問 localhost:8080/api/v1/hello 時,我收到 404 not found 錯誤,我發現了另一個與我的問題類似的問題,該問題通過在主應用程式檔案中添加組件掃描得到了修復,但如您所見,我添加了它仍然不起作用,有什么想法嗎?
uj5u.com熱心網友回復:
去掉后試試
@ComponentScan(basePackageClasses = CaseFileController.class)
uj5u.com熱心網友回復:
您的背景關系路徑可能不正確。你能不能檢查一下std應該在哪里列印出背景關系路徑,然后url是這樣的
<context-path>/api/v1/hello
uj5u.com熱心網友回復:
我建議你。
洗掉 scanpackages 因為在 spring boot 中不是必需的,spring boot 會自動掃描所有類。
將@RequestMapping("/api/v1/") 改為@RequestMapping("/api/v1")
轉載請註明出處,本文鏈接:https://www.uj5u.com/gongcheng/335975.html
