惯性聚合 高效追踪和阅读你感兴趣的博客、新闻、科技资讯
阅读原文 在惯性聚合中打开

推荐订阅源

H
Hacker News: Front Page
S
Secure Thoughts
N
News | PayPal Newsroom
Exploit-DB.com RSS Feed
Exploit-DB.com RSS Feed
T
Threatpost
N
News and Events Feed by Topic
cs.AI updates on arXiv.org
cs.AI updates on arXiv.org
A
Arctic Wolf
Cisco Talos Blog
Cisco Talos Blog
V2EX - 技术
V2EX - 技术
L
LINUX DO - 热门话题
C
Cyber Attacks, Cyber Crime and Cyber Security
P
Proofpoint News Feed
TaoSecurity Blog
TaoSecurity Blog
N
News and Events Feed by Topic
cs.CL updates on arXiv.org
cs.CL updates on arXiv.org
Hacker News - Newest:
Hacker News - Newest: "LLM"
NISL@THU
NISL@THU
H
Heimdal Security Blog
Webroot Blog
Webroot Blog
Martin Fowler
Martin Fowler
The Hacker News
The Hacker News
Engineering at Meta
Engineering at Meta
MongoDB | Blog
MongoDB | Blog
A
About on SuperTechFans
Stack Overflow Blog
Stack Overflow Blog
B
Blog RSS Feed
Vercel News
Vercel News
Blog — PlanetScale
Blog — PlanetScale
Google Online Security Blog
Google Online Security Blog
Schneier on Security
Schneier on Security
Cyberwarzone
Cyberwarzone
小众软件
小众软件
V
V2EX
K
Kaspersky official blog
Security Archives - TechRepublic
Security Archives - TechRepublic
博客园 - 三生石上(FineUI控件)
OSCHINA 社区最新新闻
OSCHINA 社区最新新闻
C
Cisco Blogs
S
Schneier on Security
Recorded Future
Recorded Future
阮一峰的网络日志
阮一峰的网络日志
AI
AI
Microsoft Security Blog
Microsoft Security Blog
H
Help Net Security
Simon Willison's Weblog
Simon Willison's Weblog
I
InfoQ
G
Google Developers Blog
博客园_首页
Hugging Face - Blog
Hugging Face - Blog

博客园 - 稻草人.Net

Python多环境管理神器pyenv+poetry python包管理利器poetry和conda使用简介 MacOS 安装Podman 替代Docker Prettier + ESLint + TS常用配置项 React Native V0.64.4版本 开发环境搭建及问题 Nodejs环境Eggjs加签验签示例 Mac安装compass失败相关问题 MySQL手动安装方法 Docker+Jenkins更换国内插件源 Chrome91版本 SameSite cookies 被移除后的解决方法 推荐几个文档中心搭建工具 前端开发Docker快速入门(二)制作镜像并创建容器 微信开放平台-第三方平台代小程序实现业务 微信开放平台-第三方平台授权流程及接口概述 Vuejs 3 Release:One Piece. Vuejs 3.0 正式版发布!代号:海贼王 尝试使用Nestjs搭建GraphQL服务 Mac安装Arduino搭建ESP8266 NodeMCU开发环境 用vscode进行jest单元测试并调试代码 vscode配置typescript和eslint的环境
Spring REST 接口自定义404不能捕获NoHandlerFoundException问题
稻草人.Net · 2023-06-27 · via 博客园 - 稻草人.Net

Spring REST 接口自定义404以及解决不能捕获NoHandlerFoundException问题  

一、自定义404响应内容

版本说明:Spring Boot 2.0.1.RELEASE

REST风格默认PostMan请求的404响应如下:

{
    "timestamp": "2018-06-07T05:23:27.196+0000",
    "status": 404,
    "error": "Not Found",
    "message": "No message available",
    "path": "/shopping/123/TEST"
}

如果不想返回此内容,可以做如下配置:

步骤一:

application.properties文件中添加如下两句:

#没有绑定的url直接抛出错误
spring.mvc.throw-exception-if-no-handler-found=true
#不为静态文件建立映射
spring.resources.add-mappings=false

添加以上配置后,404时DispatcherServlet会抛出NoHandlerFoundException,注意spring.resources.add-mappings 在当前版本下需要设置为false,否则不会抛出异常。

mappedHandler = getHandler(processedRequest);
if (mappedHandler == null || mappedHandler.getHandler() == null) {
    noHandlerFound(processedRequest, response);
    return;
}

只有第一句代码找不到对应该请求的处理器时,才会进入下面的noHandler方法去抛出NoHandlerFoundException异常。通过测试发现,springboot的WebMvcAutoConfiguration会默认配置如下资源映射:

/映射到/static(或/public、/resources、/META-INF/resources) /webjars/ 映射到classpath:/META-INF/resources/webjars/ /**/favicon.ico映射favicon.ico文件.

配置如下属性,NoHandlerFoundException异常就能被@ControllerAdvice捕获了

#不存在的url直接抛出异常
spring.mvc.throw-exception-if-no-handler-found=true
#不添加静态资源映射
spring.resources.add-mappings=false
#指定静态资源路径
spring.mvc.static-path-pattern=/statics/**
#浏览器访问时Remove Whitelabel Error Page
server.error.whitelabel.enabled=false

步骤二:

全局异常捕获通过@ExceptionHandler(NoHandlerFoundException.class)

@RestControllerAdvice
public class ExceptionResolver {

    @ExceptionHandler(Exception.class)
    public HashMap<String, String> handleException(HttpServletRequest request, Exception e) {
        HashMap<String, String> response = new HashMap<>();
        response.put("message", e.getMessage());
        return response;
    }

    @ExceptionHandler(MissingPathVariableException.class)
    public HashMap<String, String> handleMissingPathVariableException(HttpServletRequest request, MissingPathVariableException e) {
        HashMap<String, String> response = new HashMap<>();
        response.put("message", "Required path variable is missing in this request. Please add it to your request.");
        return response;
    }

    @ExceptionHandler(NoHandlerFoundException.class)
    public HashMap<String, String> handleNotFoundResourceException(HttpServletRequest request, NoHandlerFoundException e) {
        HashMap<String, String> response = new HashMap<>();
        response.put("message", "Requested resource wasn't found on the server");
        return response;
    }
}

二、注意事项

网上搜基本上都是说只需设置spring.mvc.throw-exception-if-no-handler-found=true即可,但设置后依然无效!

或者是需要设置spring.resources.add-mappings=false,但设置后依然无效!

通过看DispatcherServlet源码才发现,Srpingboot的版本不同spring.resources.add-mappings配置存在差异。

高版本比如Srping boot 2.7 要使用spring.web.resources.add-mappings=false才能生效。

另外全局异常类上要注意@RestControllerAdvice(basePackages ) 不要指定basePackages

参考文档:https://skryvets.com/blog/2018/12/27/enhance-exception-handling-when-building-restful-api-with-spring-boot/