简体   繁体   中英

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.

@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

I obtaina 404 Not Found error message.

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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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