使用背景
微服务项目当请求地址返回404和500异常的时候,希望统一拦截,而不是出现springboot 的Whitelabel Error。
使用方式
在springboot 2.3.0版本之前,可以使用实现ErrorController,然后自定义/error的url返回内容即可
如下图所票
@RestController
@RequestMapping("/error")
public class JsonErrorController extends AbstractErrorController {
public JsonErrorController(final ErrorAttributes errorAttributes) {
super(errorAttributes);
}
@GetMapping
public ResponseEntity<Map<String, Object>> error(final HttpServletRequest request) {
final Map<String, Object> body = this.getErrorAttributes(request, false);
final HttpStatus status = this.getStatus(request);
return new ResponseEntity<>(body, status);
}
@Override
public String getErrorPath() {
return "/error";
}
}
springboot 2.3.0版本以后getErrorPath已经被标记为@Deprecated
SpringBoot 2.3.0+解决方案
1.写配置
#出现错误时, 直接抛出异常
spring:
mvc:
throw-exception-if-no-handler-found: true
#不要为我们工程中的资源文件建立映射
resources:
add-mappings: false
这样就不会出现Whitelabel Error,交由@RestControllerAdvice处理
统一拦截
package vip.mate.core.web.handler;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.StringUtils;
import org.springframework.http.HttpStatus;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;
import vip.mate.core.common.api.Result;
import vip.mate.core.common.exception.BaseException;
import vip.mate.core.common.exception.PreviewException;
import vip.mate.core.common.exception.TokenException;
import java.io.FileNotFoundException;
/**
* Springboot WEB应用全局异常处理
* @author pangu
*/
@Slf4j
@ResponseBody
@RestControllerAdvice
public class BaseExceptionHandler {
/**
* BaseException 异常捕获处理
* @param ex 自定义BaseException异常类型
* @return Result
*/
@ExceptionHandler
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public Result<?> handleException(BaseException ex) {
log.error("程序异常:" + ex.toString());
return Result.fail(HttpStatus.UNAUTHORIZED.value(), ex.getMessage());
}
/**
* TokenException 异常捕获处理
* @param ex 自定义TokenException异常类型
* @return Result
*/
@ExceptionHandler
@ResponseStatus(HttpStatus.UNAUTHORIZED)
public Result<?> handleException(TokenException ex) {
log.error("程序异常==>errorCode:{}, exception:{}", HttpStatus.UNAUTHORIZED.value(), ex);
return Result.fail(HttpStatus.UNAUTHORIZED.value(), ex.getMessage());
}
/**
* FileNotFoundException,NoHandlerFoundException 异常捕获处理
* @param exception 自定义FileNotFoundException异常类型
* @return Result
*/
@ExceptionHandler({FileNotFoundException.class, NoHandlerFoundException.class})
public Result<?> noFoundException(Exception exception) {
log.error("程序异常==>errorCode:{}, exception:{}", HttpStatus.NOT_FOUND.value(), exception);
return Result.fail(HttpStatus.NOT_FOUND.value(), exception.getMessage());
}
/**
* PreviewException 空指针异常捕获处理
* @param ex 自定义PreviewException异常类型
* @return Result
*/
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<?> handleException(PreviewException ex) {
log.error("程序异常:" + ex.toString());
return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
}
/**
* NullPointerException 空指针异常捕获处理
* @param ex 自定义NullPointerException异常类型
* @return Result
*/
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<?> handleException(NullPointerException ex) {
log.error("程序异常:{}" + ex.toString());
return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), ex.getMessage());
}
/**
* 通用Exception异常捕获
* @param ex 自定义Exception异常类型
* @return Result
*/
@ExceptionHandler
@ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
public Result<?> handleException(Exception ex) {
log.error("程序异常:" + ex.toString());
String message = ex.getMessage();
if (StringUtils.contains(message, "Bad credentials")){
message = "您输入的密码不正确";
} else if (StringUtils.contains(ex.toString(), "InternalAuthenticationServiceException")) {
message = "您输入的用户名不存在";
}
return Result.fail(HttpStatus.INTERNAL_SERVER_ERROR.value(), message);
}
}
本文介绍了在Spring Boot 2.3.3.RELEASE中,由于getErrorPath被@Deprecated,如何解决Whitelabel Error页面问题。文章提供了一种配置方式和代码样例,指导如何进行统一异常拦截,避免显示默认的Spring Boot错误页面。
3833





