Home > Blockchain >  Why I obtain this "404 Not Found" error calling an API defined into a Spring Boot controll
Why I obtain this "404 Not Found" error calling an API defined into a Spring Boot controll

Time:10-25

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?

CodePudding user response:

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")

CodePudding user response:

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

  • Related