zoukankan      html  css  js  c++  java
  • 002-异常处理

    一、概述

      在spring 体系中,异常处理有如下:https://www.cnblogs.com/bjlhx/p/8666653.html

      可以结合jsr303使用:https://www.cnblogs.com/bjlhx/p/10305344.html

    二、请求参数类异常

      结合以上两种以及上文api设计原则修改响应数据,将请求参数类异常定位到http响应吗为4XX类,如下

    @ControllerAdvice
    public class GlobalExceptionHandler {
    
    
        @ExceptionHandler(value = ConstraintViolationException.class)
        @ResponseBody
        public ResponseEntity constraintViolationExceptionHandler(Exception e) {
            Map<String,Object> result=new HashMap();
            result.put("code",400);
            result.put("msg",e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
        }
    
        @ExceptionHandler(value = javax.validation.UnexpectedTypeException.class)
        @ResponseBody
        public ResponseEntity unexpectedTypeExceptionHandler(Exception e) {
            Map<String,Object> result=new HashMap();
            result.put("code",400);
            result.put("msg",e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
        }
    
        @ExceptionHandler(value = org.springframework.http.converter.HttpMessageNotReadableException.class)
        @ResponseBody
        public ResponseEntity httpMessageNotReadableExceptionHandler(Exception e) {
            Map<String,Object> result=new HashMap();
            result.put("code",400);
            result.put("msg","没有请求体,详细:"+e.getMessage());
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
        }
    
    
        @ExceptionHandler(value = org.springframework.web.bind.MethodArgumentNotValidException.class)
        @ResponseBody
        public ResponseEntity methodArgumentNotValidExceptionHandler(MethodArgumentNotValidException e) {
            Map<String,Object> result=new HashMap();
            result.put("code",400);
            result.put("msg","请求体校验失败,详细:"+e.getMessage());
            if (e.getBindingResult().hasErrors()) {
                List<FieldError> fieldErrors = e.getBindingResult().getFieldErrors();
                result.put("data",fieldErrors);
                return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
            }
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(result);
        }
        @ExceptionHandler(value = Exception.class)
        public void errorHandler(Exception e,HttpServletResponse response) throws IOException {
            response.sendError(HttpStatus.BAD_REQUEST.value());
        }
    }

    可以参看:https://blog.jayway.com/2014/10/19/spring-boot-error-responses/

  • 相关阅读:
    排序三 直接插入排序
    编写你的第一个django应用程序2
    编写你的第一个web应用程序1
    你被体制化了吗
    服务器安装tensorflow导入模块报错Illegal instruction (core dumped)
    查看数据库里有没有数据
    python实现贪吃蛇
    在pycharm中执行脚本没有报错但输出显示Redirection is not supported.
    linux安装redis
    pycharm快捷键
  • 原文地址:https://www.cnblogs.com/bjlhx/p/10329369.html
Copyright © 2011-2022 走看看