当我创建一个具有空数据(如{}或null(的员工时,我收到400和500错误。如何检查@RequestBody数据是空还是空?
@RequestMapping(value = "/add/employee", method = RequestMethod.POST)
public ResponseEntity<Object> createEmployee(@Valid @RequestBody(required = false) Employee employee){
Optional<Employee> emp = employeeService.getEmployeeById(employee.getId());
if (emp.isPresent()){
return RestResponse.generateResponse(HttpStatus.FOUND, false, "Employee exist, can't create with id: " + employee.getId(), null);
}
employeeService.createEmployee(employee);
return RestResponse.generateResponse(HttpStatus.CREATED, true, "Employee created with id: " + employee.getId(), employee);
}
我想为什么你会得到http 400或http 500错误是因为另一方面,如果你想检查你的RequestBody是空的,你的@Valid注释是空的,你可以试试这个吗?
if(employee == null)