Java自学者论坛

 找回密码
 立即注册

手机号码,快捷登录

恭喜Java自学者论坛(https://www.javazxz.com)已经为数万Java学习者服务超过8年了!积累会员资料超过10000G+
成为本站VIP会员,下载本站10000G+会员资源,会员资料板块,购买链接:点击进入购买VIP会员

JAVA高级面试进阶训练营视频教程

Java架构师系统进阶VIP课程

分布式高可用全栈开发微服务教程Go语言视频零基础入门到精通Java架构师3期(课件+源码)
Java开发全终端实战租房项目视频教程SpringBoot2.X入门到高级使用教程大数据培训第六期全套视频教程深度学习(CNN RNN GAN)算法原理Java亿级流量电商系统视频教程
互联网架构师视频教程年薪50万Spark2.0从入门到精通年薪50万!人工智能学习路线教程年薪50万大数据入门到精通学习路线年薪50万机器学习入门到精通教程
仿小米商城类app和小程序视频教程深度学习数据分析基础到实战最新黑马javaEE2.1就业课程从 0到JVM实战高手教程MySQL入门到精通教程
查看: 3888|回复: 0

spring boot 2 统一异常处理

[复制链接]
  • TA的每日心情
    奋斗
    3 天前
  • 签到天数: 792 天

    [LV.10]以坛为家III

    2049

    主题

    2107

    帖子

    72万

    积分

    管理员

    Rank: 9Rank: 9Rank: 9

    积分
    723136
    发表于 2021-4-11 03:05:33 | 显示全部楼层 |阅读模式

    spring mvc 针对controller层异常统一处理非常简单,使用 @RestControllerAdvice 或 @RestControllerAdvice 注解就可以轻@RestControllerAdvice

    public class GatewayExceptionHandler {
    
        /*@ExceptionHandler(Exception.class)
        public JsonResult handleBusinessException(HttpServletRequest request, Exception e) {
            e.printStackTrace();
            String code = ErrorCodeEnum.SYSTEM_ERROR_STRING.getCode();
            String message = StringUtils.isNotEmpty(e.getMessage()) ? e.getMessage() : "Service Currently Unavailable";
            return JsonResult.ErrorResponse(code, message);
        }*/
    
        @ExceptionHandler(value = Exception.class)
        public Map errorHandler(Exception ex) {
            Map map = new HashMap();
            map.put("code", 100);
            map.put("msg", ex.getMessage());
            return map;
        }
    }

     

    下面记录一下,spring cloud gateway项目中重写 DefaultErrorWebExceptionHandler 类,实现自定义异常处理

    首先写一个类继承 DefaultErrorWebExceptionHandler 类,重写方法

    public class RmcloudExceptionHandler extends DefaultErrorWebExceptionHandler {
    
        /**
         * Create a new {@code DefaultErrorWebExceptionHandler} instance.
         *
         * @param errorAttributes    the error attributes
         * @param resourceProperties the resources configuration properties
         * @param errorProperties    the error configuration properties
         * @param applicationContext the current application context
         */
        public RmcloudExceptionHandler(ErrorAttributes errorAttributes, ResourceProperties resourceProperties, ErrorProperties errorProperties, ApplicationContext applicationContext) {
            super(errorAttributes, resourceProperties, errorProperties, applicationContext);
        }
    
        /**
         * 确定返回什么HttpStatus
         *
         * @param errorAttributes
         * @return
         */
        @Override
        protected HttpStatus getHttpStatus(Map<String, Object> errorAttributes) {
            //HttpStatus status = (HttpStatus) errorAttributes.get("status");
            // return HttpStatus.INTERNAL_SERVER_ERROR == status ? HttpStatus.OK : status;
            return HttpStatus.OK;
        }
    
        /**
         * 返回的错误信息json内容
         *
         * @param request
         * @param includeStackTrace
         * @return
         */
        @Override
        protected Map<String, Object> getErrorAttributes(ServerRequest request, boolean includeStackTrace) {
    
            Throwable error = this.getError(request);
    
            return JsonResult.responseReturnMap(RmcloudConstant.GATEWAY_ERRORCODE, this.buildMessage(request, error));
           
        }
    
        private String buildMessage(Throwable t) {
            return "未知错误!";
        }
    
        private String buildMessage(ServerRequest request, Throwable ex) {
            StringBuilder message = new StringBuilder("api-gateway Failed to handle request [");
            message.append(request.methodName());
            message.append(" ");
            message.append(request.uri());
            message.append("]");
            if (ex != null) {
                message.append(": ");
                message.append(ex.getMessage());
            }
            return message.toString();
        }
    
        private HttpStatus determineHttpStatus(Throwable error) {
            return error instanceof ResponseStatusException ? ((ResponseStatusException) error).getStatus() : HttpStatus.INTERNAL_SERVER_ERROR;
        }
    
    }

    然后,配置自定义的ExceptionHandler

     
     
    import com.vcredit.rmcloud.gateway.exception.RmcloudExceptionHandler;
    import org.springframework.beans.factory.ObjectProvider;
    import org.springframework.boot.autoconfigure.web.ResourceProperties;
    import org.springframework.boot.autoconfigure.web.ServerProperties;
    import org.springframework.boot.context.properties.EnableConfigurationProperties;
    import org.springframework.boot.web.reactive.error.ErrorAttributes;
    import org.springframework.boot.web.reactive.error.ErrorWebExceptionHandler;
    import org.springframework.context.ApplicationContext;
    import org.springframework.context.annotation.Bean;
    import org.springframework.context.annotation.Configuration;
    import org.springframework.core.Ordered;
    import org.springframework.core.annotation.Order;
    import org.springframework.http.codec.ServerCodecConfigurer;
    import org.springframework.web.reactive.result.view.ViewResolver;

    import java.util.Collections;
    import java.util.List;

    /**
    * webflux全局异常处理器配置配置
    * 由于webflux的函数式编程方式中不能通过controllerAdvice只能通过每个RouterFunction中添加filter的方式实现异常处理,
    * 这里通过注入一个自定义ErrorWebExceptionHandler来达到全局异常处理的目的
    *
    * @author lee
    */
    @Configuration
    @EnableConfigurationProperties({ServerProperties.class, ResourceProperties.class})
    public class ErrorHandlerConfiguration {

    private final ServerProperties serverProperties;

    private final ApplicationContext applicationContext;

    private final ResourceProperties resourceProperties;

    private final List<ViewResolver> viewResolvers;

    private final ServerCodecConfigurer serverCodecConfigurer;

    public ErrorHandlerConfiguration(ServerProperties serverProperties,
    ResourceProperties resourceProperties,
    ObjectProvider<List<ViewResolver>> viewResolversProvider,
    ServerCodecConfigurer serverCodecConfigurer,
    ApplicationContext applicationContext) {
    this.serverProperties = serverProperties;
    this.applicationContext = applicationContext;
    this.resourceProperties = resourceProperties;
    this.viewResolvers = viewResolversProvider
    .getIfAvailable(Collections::emptyList);
    this.serverCodecConfigurer = serverCodecConfigurer;
    }

    @Bean
    @Order(Ordered.HIGHEST_PRECEDENCE)
    public ErrorWebExceptionHandler errorWebExceptionHandler(
    ErrorAttributes errorAttributes) {
    RmcloudExceptionHandler exceptionHandler = new RmcloudExceptionHandler(
    errorAttributes, this.resourceProperties,
    this.serverProperties.getError(), this.applicationContext);
    exceptionHandler.setViewResolvers(this.viewResolvers);
    exceptionHandler.setMessageWriters(this.serverCodecConfigurer.getWriters());
    exceptionHandler.setMessageReaders(this.serverCodecConfigurer.getReaders());
    return exceptionHandler;
    }
    }

    JsonResult内容

    @Data
    @NoArgsConstructor
    @AllArgsConstructor
    public class JsonResult<T> {
    
        private static String successCode = "";
    
        private String errorCode;
    
        private String msg;
    
        private T data;
    
        private Long timestamp;
    
        public static <T> JsonResult<T> successResponse(T data) {
            return new JsonResult<>(successCode, "Success", data, System.currentTimeMillis());
        }
    
        public static <T> JsonResult<T> errorResponse(String errorMessage) {
            return new JsonResult<>(RmcloudConstant.GATEWAY_ERRORCODE, errorMessage, null, System.currentTimeMillis());
        }
    
        public static <T> JsonResult<T> errorResponse(String status, String errorMessage) {
            return new JsonResult<>(status, errorMessage, null, System.currentTimeMillis());
        }
    
        public static Map<String, Object> responseReturnMap(String status, String errorMessage) {
            Map<String, Object> map = new HashMap<>();
            map.put("errorCode", status);
            map.put("msg", errorMessage);
            map.put("data", null);
            return map;
        }
    }

     

    最后感谢chenqian56131,主要代码是从他github上淘来的,以上是结合实际项目的应用,记录下来,方便以后查阅。

     

    哎...今天够累的,签到来了1...
    回复

    使用道具 举报

    您需要登录后才可以回帖 登录 | 立即注册

    本版积分规则

    QQ|手机版|小黑屋|Java自学者论坛 ( 声明:本站文章及资料整理自互联网,用于Java自学者交流学习使用,对资料版权不负任何法律责任,若有侵权请及时联系客服屏蔽删除 )

    GMT+8, 2024-9-17 05:08 , Processed in 0.057915 second(s), 29 queries .

    Powered by Discuz! X3.4

    Copyright © 2001-2021, Tencent Cloud.

    快速回复 返回顶部 返回列表