简体   繁体   English

为什么我在调用定义到 Spring Boot 控制器类中的 API 时出现“404 Not Found”错误?

[英]Why I obtain this "404 Not Found" error calling an API defined into a Spring Boot controller class?

I am working on a Spring Boot application and I added this very simple controller class to my project in order to implement some APIs.我正在开发一个 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);
    }

}

the problem is that running my application I obtain no error but then when I try to access to this API performing a GET call to this address (via browser or PostMan): http://localhost/api/jobs问题是运行我的应用程序我没有得到错误但是当我尝试访问这个 API 时执行 GET 调用这个地址(通过浏览器或邮递员):http://localhost/api/jobs

I obtaina 404 Not Found error message.我获得404 Not Found错误消息。

Why?为什么? What am I missing?我错过了什么? What can I try to check to solve this issue?我可以尝试检查什么来解决这个问题?

You are missing the port.您缺少端口。 For Spring Boot the default is 8080 so your call would be to http://localhost:8080/api/jobs/barcodeVariable对于 Spring Boot,默认值为 8080,因此您的调用将是 http://localhost:8080/api/jobs/barcodeVariable

Also, you need to include the path variable in the path in order to use it.此外,您需要在路径中包含路径变量才能使用它。 So the method annotation would be所以方法注释将是

@GetMapping(value = "/jobs/{barcode}", produces = "application/json")

Try this:尝试这个:

@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);
    }

}

You are taking a path variable but not providing it while making a request.您正在使用路径变量,但在发出请求时未提供它。 So, either remove the path variable or try requesting like this: localhost:8080/api/jobs/barcode因此,要么删除路径变量,要么尝试像这样请求:localhost:8080/api/jobs/barcode

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM