想起之前发过一篇spring统一返回的文章,最后提到404异常是抓不到的。这里我们将测试@RestControllerpublicclassTestController{@GetMapping("/test")publicStringinsert22(){return"hello";}}浏览器请求尝试http://localhost:8080/xxxerror#WhitelabelErrorPageThisapplicationhasnoexplicitmappingfor/error,soyouareseeingthisasafallback.WedDec2910:14:36CST2021Therewas意外错误(类型=未找到,状态=404)。springboot的处理方式springboot处理这个404的异常在BasicErrorController@Controller@RequestMapping("${server.error.path:${error.path:/error}}")publicclassBasicErrorControllerextendsAbstractErrorController{...........@OverridepublicStringgetErrorPath(){返回空值;}@RequestMapping(produces=MediaType.TEXT_HTML_VALUE)publicModelAndViewerrorHtml(HttpServletRequestrequest,HttpServletResponseresponse){HttpStatusstatus=getStatus(request);Map模型=集合.unmodifiableMap(getErrorAttributes(请求,getErrorAttributeOptions(请求,MediaType.TEXT_HTML)));response.setStatus(status.value());ModelAndViewmodelAndView=resolveErrorView(请求、响应、状态、模型);返回(modelAndView!=null)?modelAndView:newModelAndView("错误",模型);}//包含请求头“Accept”:“application/json”将放在这里@RequestMappingpublicResponseEntity