当我尝试 localhost:8080/api/employees 时,我得到一个列表(JSON 格式)。我还想通过 ID 获得一个员工。当我尝试 localhost:8080/api/123abc 时,我找不到具有该 ID 的员工。我的回应是:
Whitelabel 错误页面 此应用程序没有针对 /error 的显式映射,因此您将其视为后备。
2020 年 7 月 28 日星期二 08:50:28 CEST 出现意外错误(类型=未找到,状态=404)。
我的代码在下面
@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);
}
编辑:@PathVariable Long id
也poller.getEmployeeById(id.toString());
不起作用。