异常的统一处理(zalando)

一、依赖引入

 		<dependency>
            <groupId>org.zalando</groupId>
            <artifactId>problem-spring-web</artifactId>
            <version>0.26.1</version>
        </dependency>

二、配置两个ExceptionHandler

2.1、ExceptionHandler

package com.moss.uaa_security.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.zalando.problem.spring.web.advice.ProblemHandling;

/**
 * @description
 * @author: lwj
 * @create: 2021-03-25 23:34
 **/
@ControllerAdvice
public class ExceptionHandler implements ProblemHandling {

    /**
     * 是否将堆栈中的错误信息返回
     *
     * @return
     */
    @Override
    public boolean isCausalChainsEnabled() {
        return true;
    }
}

2.2、SecurityExceptionHandler

package com.moss.uaa_security.exception;

import org.zalando.problem.spring.web.advice.security.SecurityAdviceTrait;

/**
 * @description
 * @author: lwj
 * @create: 2021-03-25 23:36
 **/
public class SecurityExceptionHandler implements SecurityAdviceTrait {
}

三、配置使用zalando的problem处理security的请求异常

在这里插入图片描述

四、中文乱码处理

如果在测试中发现出现中文乱码,则配置下面servlet.encoding.force: true

server:
  error:
    whitelabel:
      enabled: true
  servlet:
    encoding:
      force: true

版权声明:本文为weixin_38657051原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。