zoukankan      html  css  js  c++  java
  • springboot全局异常处理

    @Slf4j
    @ControllerAdvice
    public class RestExceptionHandler extends ResponseEntityExceptionHandler {

    @ExceptionHandler({InvalidRequestException.class})
    public ResponseEntity<?> handleInvalidRequest(HttpServletRequest request, InvalidRequestException e) {
    log.warn("invalid request exception {}", e);
    return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(e.getErrorCode().isForm() ? HttpStatus.GONE : HttpStatus.CONFLICT, "logic error", e.getErrorCode() == null ? null : e.getErrorCode().getCode(),
    e.getMessage() == null ? ResponseErrorCode.GENERAL_PARAMS_ERROR.getDescription() : e.getMessage(), request.getServletPath()), "**"));
    }

    @ExceptionHandler({FormulaErrorException.class})
    public ResponseEntity<?> handleFormula(HttpServletRequest request, FormulaErrorException e) {
    log.warn("invalid request exception {}", e);
    return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.GONE, "logic error", ResponseErrorCode.CONFIG_FORMULA_ERROR.getCode(),
    e.getMessage() == null ? ResponseErrorCode.CONFIG_FORMULA_ERROR.getDescription() : e.getMessage(), request.getServletPath()), "**"));
    }

    @ExceptionHandler(value = {AccessDeniedException.class})
    public ResponseEntity<?> handlerAccessDeniedException(HttpServletRequest request, HttpServletResponse response, AccessDeniedException ex) throws IOException {
    /* if (Objects.equals(ex.getMessage(), "不允许访问")) {
    return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
    .body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.UNAUTHORIZED,
    "Forbidden", "no authority", request.getServletPath())));
    } else {
    return ResponseEntity.status(HttpStatus.FORBIDDEN)
    .body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.FORBIDDEN,
    "Forbidden", "Access Denied", request.getServletPath())));
    }*/
    return ResponseEntity.status(HttpStatus.UNAUTHORIZED)
    .body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.UNAUTHORIZED,
    "Forbidden", "no authority", request.getServletPath())));
    }

    @ExceptionHandler
    public ResponseEntity<?> handleException(HttpServletRequest request, Exception e) {
    log.warn("invalid request exception {}", e);
    return ResponseEntity.status(HttpStatus.CONFLICT).body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.CONFLICT, "system error", ResponseErrorCode.GENERAL_UNKNOWN_AUTHORITY.getCode(),
    e.getMessage() == null ? ResponseErrorCode.GENERAL_UNKNOWN_AUTHORITY.getDescription() : e.getMessage(), request.getServletPath()), "**"));
    }

    @Override
    protected ResponseEntity<Object> handleMethodArgumentNotValid(MethodArgumentNotValidException ex, HttpHeaders headers, HttpStatus status, WebRequest request) {
    List<FieldError> fieldErrors = ex.getBindingResult().getFieldErrors();
    List<ObjectError> globalErrors = ex.getBindingResult().getGlobalErrors();
    List<String> errors = new ArrayList<>(fieldErrors.size() + globalErrors.size());
    String error;
    for (FieldError fieldError : fieldErrors) {
    error = fieldError.getField() + ", " + fieldError.getDefaultMessage();
    errors.add(error);
    }
    for (ObjectError objectError : globalErrors) {
    error = objectError.getObjectName() + ", " + objectError.getDefaultMessage();
    errors.add(error);
    }
    return ResponseEntity.status(HttpStatus.CONFLICT)
    .body(new AntPathFilterMappingJacksonValue(new ErrorResponseMessage(HttpStatus.GONE,
    "invalidate parameter", ResponseErrorCode.GENERAL_PARAMS_ERROR.getCode(),
    StringUtils.join(errors), ((ServletWebRequest) request).getRequest().getServletPath())));
    }

    }
  • 相关阅读:
    CV方向的高效阅读英文文献方法总结
    数据增强方法总结
    CNN结构演变总结(三)设计原则
    CNN结构演变总结(二)轻量化模型
    CNN结构演变总结(一)经典模型
    CNN可视化技术总结(四)--可视化工具与项目
    Codeforces972 D. Kuro and GCD and XOR and SUM 01Trie
    Codeforces 982 D. Shark
    Codeforces Round #700 (Div. 2) A~D题解
    codeforces 1004 D. Sonya and Matrix 构造
  • 原文地址:https://www.cnblogs.com/xifenglou/p/8727127.html
Copyright © 2011-2022 走看看