springboot訪問不存在的URL時的處理方法
引言
在前后端分離的模式下,當(dāng)Spring Boot應(yīng)用接收到一個不存在的URL請求時,通常希望返回一個固定的JSON字符串作為響應(yīng),以便前端能夠據(jù)此進行相應(yīng)的處理。
最初的思路是自定義一個錯誤控制器來處理404錯誤,并返回一個JSON格式的響應(yīng)體。讓錯誤控制器實現(xiàn)ErrorController接口,并重寫getErrorPath()方法以指定錯誤處理的路徑。然后,你可以在這個控制器中創(chuàng)建一個處理404錯誤的方法。
然而卻發(fā)現(xiàn)springboot3.3.5中ErrorController沒有任何接口方法,只能另辟蹊徑,考慮使用ErrorPageRegistrar。
ErrorPageRegistrar
ErrorPageRegistrar是Spring Boot中用于注冊錯誤頁面的接口。通過實現(xiàn)這個接口,可以自定義不同HTTP狀態(tài)碼對應(yīng)的錯誤頁面。
ErrorPageRegistrar接口定義在Spring Boot的Web模塊中,它包含一個方法registerErrorPages(ErrorPageRegistry registry)。這個方法用于注冊錯誤頁面,其中ErrorPageRegistry是一個用于添加錯誤頁面的注冊表。
實現(xiàn)接口
創(chuàng)建一個類實現(xiàn)ErrorPageRegistrar接口,并重寫registerErrorPages方法。在這個方法中,可以使用ErrorPageRegistry的addErrorPages方法來添加多個錯誤頁面。
import org.springframework.boot.web.server.ErrorPage; import org.springframework.boot.web.server.ErrorPageRegistrar; import org.springframework.boot.web.server.ErrorPageRegistry; import org.springframework.context.annotation.Configuration; import org.springframework.http.HttpStatus; /** * @基本功能: * @program:ecconfigcenter * @author:Jet * @create:2024-11-07 17:29:58 **/ @Configuration public class ErrorPageConfig implements ErrorPageRegistrar { @Override public void registerErrorPages(ErrorPageRegistry registry) { // 注冊404錯誤頁面 ErrorPage error404 = new ErrorPage(HttpStatus.NOT_FOUND, "/ec/error/404"); // 注冊500錯誤頁面 ErrorPage error500 = new ErrorPage(HttpStatus.INTERNAL_SERVER_ERROR, "/ec/error/500"); // 將錯誤頁面添加到注冊表中 registry.addErrorPages(error404, error500); } }
這里使用@Configuration注解標(biāo)記ErrorPageConfig為配置類,以便Spring Boot能夠自動掃描并注冊這個配置。
在registerErrorPages方法中,通過創(chuàng)建ErrorPage對象來指定HTTP狀態(tài)碼和對應(yīng)的錯誤處理的路徑。然后,將這些ErrorPage對象傳遞給addErrorPages方法。
注冊錯誤處理
import cn.com.ec.eccommon.common.BaseController; import lombok.extern.slf4j.Slf4j; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMethod; import org.springframework.web.bind.annotation.ResponseBody; import java.util.HashMap; import java.util.Map; /** * @基本功能: * @program:ecconfigcenter * @author:Jet * @create:2024-11-07 17:29:58 **/ @Slf4j @RequestMapping("/ec") @Controller public class CustomErrorController extends BaseController { /** * 404處理 * @return */ @RequestMapping(value = "/error/404",method = RequestMethod.POST) @ResponseBody public Map<String,Object> handle404() { Map<String,Object>result = new HashMap<>(); result = this.setJson(404,"url is invalid.",null); return result; } /** * 500處理 * @return */ @RequestMapping(value = "/error/500",method = RequestMethod.POST) @ResponseBody public Map<String,Object> handle500() { Map<String,Object>result = new HashMap<>(); result = this.setJson(500,"Server internal error.",null); return result; } }
到此這篇關(guān)于springboot訪問不存在的URL時的處理方法的文章就介紹到這了,更多相關(guān)SpringBoot訪問不存在的URL內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Elasticsearch?mapping?概念及自動創(chuàng)建示例
這篇文章主要為大家介紹了Elasticsearch?mapping?概念及自動創(chuàng)建示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11SpringMVC事件監(jiān)聽ApplicationListener實例解析
這篇文章主要介紹了SpringMVC事件監(jiān)聽ApplicationListener實例解析,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友可以參考下2019-11-11