現(xiàn)如今每一個(gè)網(wǎng)站都會(huì)有自己的404頁面盔几,但是作為一個(gè)純后端的應(yīng)用晴弃,肯定是沒有靜態(tài)資源的,這輩子也不可能會(huì)有靜態(tài)資源
對(duì)于Spring MVC它有自己的一套404返回逊拍,例如這樣
{
"timestamp": "2018-09-26T17:03:41.161+0800",
"status": 404,
"error": "Not Found",
"message": "No message available",
"path": "/process-instance/overview1"
}
但是肯定不是我們所希望的
如果使用的是Spring Boot上鞠,可以在application.properties
中設(shè)置
spring.mvc.throw-exception-if-no-handler-found=true
該參數(shù)對(duì)應(yīng)的就是DispatcherServlet
中的throwExceptionIfNoHandlerFound
再在全局異常處理中利用@ExceptionHandler
捕獲NoHandlerFoundException
就可以了
但是并沒有生效,原因是Spring會(huì)默認(rèn)給你加上ResourceHttpRequestHandler
這個(gè)handler顺献,也就不會(huì)出現(xiàn)noHandler
的情況了旗国,該handler是用來處理資源使用的
spring.resources.add-mappings=false
如上配置就可以了