Controller层
return patientRepository.findById(id)
.orElseThrow(() -> new HandleException(String.format("Patient %d handle error", id)));
Exception类
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public class HandleException extends RuntimeException {
public HandleException(String message) {
super(message);
}
}
Json响应
{
"timestamp": 1481488691203,
"status": 500,
"error": "...",
"exception": "...",
"message": "....",
"path": "/patients/1"
}
总结
还可以使用@ExceptionHandler
手动处理异常,并手动设置响应json。