
本文共 7246 字,大约阅读时间需要 24 分钟。
Spring Boot ������������
��������� Spring Boot ������������
������
������������������������
1. ������ @ExceptionHandler ���������������������
1. ������������������
���controller��������������� @ExceptionHandler ��������������������������������������������������������������������������� value ������������������������������������������
@RestController@RequestMapping("ex")@Api(tags = {"������������������������"})public class TestPartExceptionController { @GetMapping("ex3")@ApiOperation(value = "������������������������", httpMethod = "GET")public void testPartException() throws Exception { int i = 1 / 0; // ������������������}@ExceptionHandler(value = {Exception.class})public ResponseEntityhandlerPartException(HttpServletRequest request, HttpServletResponse response, Exception e) { return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR) .body(ResultBean.result(HttpStatus.INTERNAL_SERVER_ERROR));} }
������������������ testPartException ������������������������������ handlerPartException ������������������������ Exception ��������������������������������� Exception ������������������������������������
������ @ExceptionHandler ��� @RestControllerAdvice��������� @ControllerAdvice���������������������������
������������������������
���������������@RestControllerAdvicepublic class GlobalExceptionHandler { @ExceptionHandler(value = {Exception.class})public ResultBean handleException(HttpServletRequest request, HttpServletResponse response, Exception e, HandlerMethod method) throws Exception { String message = e.getMessage(); log.error("error -- message = " + message, e); return ResultBean.error(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);} }
������ @RestControllerAdvice ���������������������������������@RestControllerAdvice ��� value ������ basePackages ��������������������������������������������� handleException ��������������������������������������������� Exception ��������������������������������������������������������� @RestControllerAdvice ������������ @ExceptionHandler ��������������������������������������������������������������������������������������������������������������� Exception ������������������������������
������������ Spring Boot ������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������������
������������������������
������ MethodArgumentNotValidException ��� ConstraintViolationException ������������
���������������������������������������������������MethodArgumentNotValidException������������ Bean ������������������������
ConstraintViolationException��������������������������������� String���������������������������
���������������
@ExceptionHandler(value = {ConstraintViolationException.class, MethodArgumentNotValidException.class})public ResponseEntity constraintValidatedExceptionHandler(HttpServletRequest request, Exception e, HandlerMethod method) { ResponseEntity.BodyBuilder responseEntityBuilder = ResponseEntity.badRequest(); ResultBean resultBean = ResultBean.result(HttpStatus.BAD_REQUEST); StringJoiner stringJoiner = new StringJoiner(";", "������������{ ", " }");if (e instanceof ConstraintViolationException) { ConstraintViolationException constraintViolationException = (ConstraintViolationException) e; Optional.ofNullable(constraintViolationException.getConstraintViolations()).orElseGet(List::new) .forEach(v -> { PathImpl propertyPath = (PathImpl) v.getPropertyPath(); String s = propertyPath.getLeafNode().asString(); stringJoiner.add(s + ": " + v.getMessage()); });}if (e instanceof MethodArgumentNotValidException) { MethodArgumentNotValidException methodArgumentNotValidException = (MethodArgumentNotValidException) e; ListfieldErrors = methodArgumentNotValidException.getBindingResult().getFieldErrors(); Optional.ofNullable(fieldErrors).orElseGet(List::new) .forEach(v -> { String field = v.getField(); String defaultMessage = v.getDefaultMessage(); stringJoiner.add(field + ": " + defaultMessage); });}resultBean.setMsg(stringJoiner.toString());return responseEntityBuilder.body(resultBean); }
������������������������������������������������������������������������������������������������������������������������������������������Swagger ������������������������������������������������������������
���������������������
��������������������� Businessexception������������������������ BusinessException���
���������������@ExceptionHandler(value = {BusinessException.class})public void businessExceptionHandler(HttpServletRequest request, HttpServletResponse response, BusinessException e, HandlerMethod method) throws IOException { log.error("��������������� businessException;' + "���������������������{}; ������URI���{};' + "���������������{}��� error message���{}", request.getRemoteHost(), request.getRequestURI(), method.getMethod().getName(), e.getLocalizedMessage()); response.getWriter().write(new ObjectMapper().writeValueAsString( ResultBean.error(e.getCode(), e.getMsg()) ));}
���������������
@GetMapping("ex")@ApiOperation(value = "���������������������������", httpMethod = "GET")public Object testMyException(@RequestParam(required = false) @ApiParam(name = "str", value = "str") String str) throws Exception { throw new BusinessException("test businessException", 5001);}
��������������������������������������������������� BusinessException ������������������������������������������ Swagger ���������������������������������������������������������������������������������������������
2. ������������ ResponseEntityExceptionHandler ������������������������
������������������ spring ��������� default ��������������������������������� ResponseEntityExceptionHandler ������������������������������������������������������������
���������������@RestControllerAdvicepublic class GlobalExHandler extends ResponseEntityExceptionHandler { @Overrideprotected ResponseEntity
��������������������������������������������������� default ���������������������������������������������������������������������������
������������������������ Spring Boot ������������������������������ @ExceptionHandler ������������������������������������������������������������������������������������������������������������������������ Spring Boot ��� Lombok ������������������������������������������
发表评论
最新留言
关于作者
