0
点赞
收藏
分享

微信扫一扫

【Java】java | springboot | 全局异常处理 | 拦截404


一、说明

        1、maven项目

        2、springboot项目

        3、走统一异常处理

二、代码

1、拦截404

1)直接copy

@ExceptionHandler(NoHandlerFoundException.class)
public AjaxResult handleNoHandlerFoundException(NoHandlerFoundException e, HttpServletRequest request){
log.error("handleNoHandlerFoundException(),请求404,uri:{}", request.getRequestURI());
return AjaxResult.error(HttpStatus.NOT_FOUND, "not find your request");
}

说明:AjaxResult换成响应实体即可

2)application.yml需要增加配置

spring: 
mvc:
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false

说明: 没有找到uri的处理器,抛弃异常;不使用spring默认静态资源映射

说明: 如果用了swagger,建议暂时不拦截404,会导致swagger-ui.html失效;

~~

举报

相关推荐

0 条评论