![](/img/trans.png)
[英]Why Spring Boot controller test returning 404, but controller found in context?
[英]Why I obtain this "404 Not Found" error calling an API defined into a Spring Boot controller class?
我正在開發一個 Spring Boot 應用程序,我將這個非常簡單的控制器類添加到我的項目中以實現一些 API。
@RestController
@RequestMapping("/api")
public class JobStatusApi {
@Autowired
ListableJobLocator jobLocator;
@GetMapping(value = "/jobs", produces = "application/json")
public ResponseEntity<List<String>> listArtByEan(@PathVariable("barcode") String Barcode) {
List<String> jobsList = (List<String>) jobLocator.getJobNames();
return new ResponseEntity<List<String>>(jobsList, HttpStatus.OK);
}
}
問題是運行我的應用程序我沒有得到錯誤但是當我嘗試訪問這個 API 時執行 GET 調用這個地址(通過瀏覽器或郵遞員):http://localhost/api/jobs
我獲得404 Not Found錯誤消息。
為什么? 我錯過了什么? 我可以嘗試檢查什么來解決這個問題?
您缺少端口。 對於 Spring Boot,默認值為 8080,因此您的調用將是 http://localhost:8080/api/jobs/barcodeVariable
此外,您需要在路徑中包含路徑變量才能使用它。 所以方法注釋將是
@GetMapping(value = "/jobs/{barcode}", produces = "application/json")
嘗試這個:
@RestController
@RequestMapping("/api")
public class JobStatusApi {
@Autowired
ListableJobLocator jobLocator;
@GetMapping(value = "/jobs", produces = MediaType.APPLICATION_JSON_VALUE)
public ResponseEntity<List<String>> listArtByEan(String Barcode) {
List<String> jobsList = (List<String>) jobLocator.getJobNames();
return new ResponseEntity<List<String>>(jobsList, HttpStatus.OK);
}
}
您正在使用路徑變量,但在發出請求時未提供它。 因此,要么刪除路徑變量,要么嘗試像這樣請求:localhost:8080/api/jobs/barcode
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.