在 Spring 6,ProblemDetail可用
@ExceptionHandler(EntityNotFoundException.class)
protected ProblemDetail handleEntityNotFoundException(EntityNotFoundException ex, HttpServletRequest request) {
ProblemDetail problemDetail = ProblemDetail.forStatusAndDetail(HttpStatus.NOT_FOUND, ex.getMessage());
problemDetail.setTitle("entity.not.found");
return problemDetail;
}
除了直接返回problemDetail之外,是否有任何实用程序可以返回
ResponseEntity<ProblemDetail>
1条答案
按热度按时间nx7onnlm1#
有很多方法!
使用
@ResponseBody
:使用Spring
ResponseEntity
static
模式:支持的其他状态:
自定义
body
和header
的示例:或者使用传统构造函数和自定义状态:
参考: