zoukankan      html  css  js  c++  java
  • Restful接口调用统一异常处理

    1.关键字解释

    //它是一个Controller增强器,可对controller中被 @RequestMapping注解的方法加一些逻辑处理
    @ControllerAdvice
    //异常定义
    @ExceptionHandler
    //返回格式为json,可以使用 @RestControllerAdvice 代替 @ControllerAdvice,这样在方法上就可以不需要添加 @ResponseBody
    @ResponseBody

    2.springmvc对于http请求的异常类型

    Exception Type

    HTTP Status Code 

    ConversionNotSupportedException

    500 (Internal Server Error) 

    HttpMediaTypeNotAcceptableException

     

    406 (Not Acceptable)  

    HttpMediaTypeNotSupportedException

     

    415 (Unsupported Media Type) 

    HttpMessageNotReadableException

     

    400 (Bad Request) 

    HttpMessageNotWritableException

     

     500 (Internal Server Error) 

    HttpRequestMethodNotSupportedException

     

    405 (Method Not Allowed) 

    MissingServletRequestParameterException

    400 (Bad Request)  

     

    NoSuchRequestHandlingMethodException

     

    404 (Not Found)  

     

    TypeMismatchException

     

    400 (Bad Request)



    3.代码

    @ControllerAdvice
    public class RestfulApiExceptionHandler {
        /**
         * 缺失参数
         * @param request
         * @param exception
         * @return
         */
        @ExceptionHandler(value = MissingServletRequestParameterException.class)
        @ResponseBody
        public Response missingParameterExceptionHandler(HttpServletRequest request, MissingServletRequestParameterException exception){
            return Response.failure("缺少必要参数,参数名称为" + exception.getParameterName());
        }
    
        /**
         * 参数类型不匹配
         * @param request
         * @param exception
         * @return
         */
        @ExceptionHandler({TypeMismatchException.class})
        @ResponseBody
        public Response typeMismatchExceptionHandler(HttpServletRequest request,TypeMismatchException exception){
            return Response.failure("参数类型不匹配,类型应该为" + exception.getRequiredType());
        }
    
        /**
         * 请求方法不支持
         * @param request
         * @param exception
         * @return
         */
        @ExceptionHandler(value = HttpRequestMethodNotSupportedException.class)
        @ResponseBody
        public Response methodNotSupportedExceptionHandler(HttpServletRequest request,HttpRequestMethodNotSupportedException exception){
            return Response.failure("不支持的请求方法");
        }
        /**
         * 其他异常
         * @param request
         * @param exception
         * @return
         */
        @ExceptionHandler(value = Exception.class)
        @ResponseBody
        public Response exceptionHandler(HttpServletRequest request, Exception exception){
            return Response.failure("系统异常");
        }
    }
  • 相关阅读:
    20000字干货笔记,一天搞定Mysql~【转】
    Linux操作系统概述及内核介绍
    如何在装有高版本NBU的主机上安装低版本的NBU?卸载8.0安装7.5记录
    vmware+kvm+vnc安装配置
    NBU异机恢复Oracle数据库,作业报错2850处理
    NetBackup 进程整理
    1、虚拟化实施流程、宿主机如何选型、如何进行性能测试
    灾难恢复的衡量指标RTO和RPO
    国内主要灾备厂商
    单例设计模式
  • 原文地址:https://www.cnblogs.com/i-tao/p/13846999.html
Copyright © 2011-2022 走看看