500错误页面之前可以xml中配置errorpage的配置,或者tomcat的web.xml中处理,现在可以进行统一处理。
新建处理类统一处理
@ControllerAdvice
public class ExceptionHandler {
@org.springframework.web.bind.annotation.ExceptionHandler(value = RuntimeException.class)
public @ResponseBody Object errorHandlerByJson(HttpServletRequest request,Exception e) throws Exception{
//可以拿到异常信息
e.printStackTrace();
//可以返回统一数据
return "服务器开小差";
}
}
@Controller
public class ExceptionController {
@RequestMapping("/hello")
public @ResponseBody String hello(){
int i = 10/0;
return "hello join";
}
}
![](/i/l/?n=23&i=blog/1446099/202303/1446099-20230322174108504-356425895.png)
标签:xml,springboot,处理,hello,异常,class,统一 From: https://www.cnblogs.com/healthinfo/p/17244910.html