Reputation: 133
When I try localhost:8080/api/employees I get a list (JSON-format). I would also like to get a single employee by ID. When I try localhost:8080/api/123abc I cannot find the employee with that ID. My response is:
Whitelabel Error Page This application has no explicit mapping for /error, so you are seeing this as a fallback.
Tue Jul 28 08:50:28 CEST 2020 There was an unexpected error (type=Not Found, status=404).
My code is below here
@RestController
@RequestMapping(value = "/api", produces = MediaType.APPLICATION_JSON_VALUE)
public class TestApiController {
@Autowired
private EmployeePoller poller;
@GetMapping(path = "/employees")
public List<Employee> allEmployees() {
return poller.getAllEmployees();
}
@GetMapping(path = "/{id}")
public Employee singleEmployee(@PathVariable String id) {
return poller.getEmployeeById(id);
}
edit: @PathVariable Long id
and poller.getEmployeeById(id.toString());
doesn't work either.
Upvotes: 2
Views: 345
Reputation: 36
The 404 - Not found could be:
To confirm what's your case, do a new request with method OPTION to localhost:8080/api/123abc
If the response is 404, the issue is in your controller. If response is 200, then there isn't employee with id 123abc.
Also I see you are using the same path for both endpoints. Try the following code (It validate if "id" variable is employees).
@GetMapping(path = "/{id}")
public Employee getEmployee(@PathVariable(name = "id") String id) {
if ("employees".equals(id)) {
return poller.getAllEmployees();
} else {
return poller.getEmployeeById(id);
}
}
Upvotes: 1