當(dāng)前位置:
首頁 >
前端技术
> javascript
>内容正文
javascript
Spring Boot笔记-404错误统一管理
生活随笔
收集整理的這篇文章主要介紹了
Spring Boot笔记-404错误统一管理
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
這里有一點要注意:
spring boot錯誤的頁面都會到error的請求中,所以可以用如下的方式統(tǒng)一管理
package com.xxxxx.apc.controller;import com.apc.apc.utils.ResultUtil; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;import javax.servlet.http.HttpServletRequest; import java.util.HashMap; import java.util.Map;@RestController public class ErrorController implements org.springframework.boot.autoconfigure.web.ErrorController {@Overridepublic String getErrorPath() {return "error";}@RequestMapping("/error")public Object handleError(HttpServletRequest request){Integer statusCode = (Integer) request.getAttribute("javax.servlet.error.status_code");if(statusCode == 404){Map<String, String> map = new HashMap<>();map.put("tip", "can not find page");return ResultUtil.error(map);}return null;}}運行截圖如下:
總結(jié)
以上是生活随笔為你收集整理的Spring Boot笔记-404错误统一管理的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: Leaflet笔记-Leaflet与ec
- 下一篇: Python笔记-Flask的搭建及基本