所以这是我当前对rest-api端点的实现,我想处理一些极端情况,例如找不到用户或单位,所以我会抛出适当的异常,但是如何显示在控制器中处理?现在它还没有真正起作用,如果我设置了一个不存在的id,它将照常工作,并且我没有收到对应的错误消息。
服务层:
public void delete(Long flatId) {
flatRepository.findById(flatId).ifPresentOrElse(flat -> {
List<User> residents = flat.getResidents();
residents.forEach(resident -> resident.setFlat(null));
flatRepository.delete(flat);
},
() -> new ResourceNotFoundException("Flat " + flatId + " found"));
}
控制器层:
@DeleteMapping("/flats/{flatId}")
public void deleteFlat(@PathVariable Long flatId) {
flatService.delete(flatId);
}
GlobalExceptionHandler:
@ControllerAdvice
@RestController
public class GlobalExceptionHandler extends ResponseEntityExceptionHandler {
@ExceptionHandler(ResourceNotFoundException.class)
@ResponseStatus(HttpStatus.NOT_FOUND)
public final ErrorDetails handleResourceNotFoundException(ResourceNotFoundException ex) {
return new ErrorDetails(LocalDateTime.now(), ex.getMessage(), 404);
}
@ExceptionHandler(ResourceAlreadyDefinedException.class)
@ResponseStatus(HttpStatus.CONFLICT)
public final ErrorDetails handleResourceAlreadyDefinedException(ResourceAlreadyDefinedException ex) {
return new ErrorDetails(LocalDateTime.now(), ex.getMessage(), 409);
}
}
更新:我创建了这个全局异常处理程序,但是如果我使用不存在的id向我的api发送删除请求,它将不会发送404信息给我,只会回复200。而如果我有一个返回值,例如在这种情况下,它会按预期工作。
public Flat get(Long id) {
return flatRepository.findById(id).orElseThrow(() -> new ResourceNotFoundException("Flat " + id + " not found"));
}
@GetMapping("/flats/{flatId}")
public ResponseEntity<Flat> getFlat(@PathVariable Long flatId) {
return ResponseEntity.ok(flatService.get(flatId));
}