标签:方法 json string 求和 静态资源 image 模板 ESS lte
接着上一篇博客,继续分析Springboot错误页面问题
//1-自定义Exception public class DataException extends RuntimeException { public DataException() { super("数据不存在!"); } } //2-自定义handleException方法 @ControllerAdvice public class MyExceptionHandler { @ResponseBody @ExceptionHandler(DataException.class) public Map<String,Object> handleException(Exception e){ Map<String,Object> map = new HashMap<String,Object>(); map.put("code","data error"); map.put("msg",e.getMessage()); return map; } } //3-测试接口 @Controller public class DemoController { @GetMapping(value="test") public String toExceptionPage( ){ throw new DataException(); } }
//1-自定义Exception public class DataException extends RuntimeException { public DataException() { super("数据不存在!"); } } //2-自定义handleException方法 @ControllerAdvice public class MyExceptionHandler { @ExceptionHandler(DataException.class) public String handleException(Exception e, HttpServletRequest request){ //传入我们自己的错误状态码 4xx 5xx,否则就不会进入定制错误页面的解析流程 request.setAttribute("javax.servlet.error.status_code",500); Map<String,Object> map = new HashMap<String,Object>(); map.put("code","data error"); map.put("msg",e.getMessage()); return "forward:/error"; } } //3-测试接口 @Controller public class DemoController { @GetMapping(value="test") public String toExceptionPage( ){ throw new DataException(); } }
//1-自定义ErrorAttributes @Component public class MyErrorAttributes extends DefaultErrorAttributes { //重写getErrorAttributes方法-添加自己的项目数据 public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) { Map<String, Object> errorAttributes = super.getErrorAttributes(webRequest,includeStackTrace); errorAttributes.put("myName","我不吃番茄");//自定义数据 errorAttributes.put("myAge","不告诉你");//自定义数据 return errorAttributes; } } //2-自定义Exception public class DataException extends RuntimeException { public DataException() { super("数据不存在!"); } } //3-自定义handleException方法 @ControllerAdvice public class MyExceptionHandler { @ExceptionHandler(DataException.class) public String handleException(Exception e, HttpServletRequest request){ //传入我们自己的错误状态码 4xx 5xx,否则就不会进入定制错误页面的解析流程 request.setAttribute("javax.servlet.error.status_code",500);//这里只接受500状态错误 Map<String,Object> map = new HashMap<String,Object>(); map.put("code","data error"); map.put("msg",e.getMessage()); request.setAttribute("extra", map);//放在request中的数据,在前端页面中都可以取出来 return "forward:/error";//并不直接返回视图名称或json数据,请求转发到"/error",让Springboo按流程处理处理,从而达到自适应浏览器请求和客户端请求; } } //4-测试接口 @Controller public class DemoController { @GetMapping(value="test") public String toExceptionPage( ){ throw new DataException();//主动抛出一个500错误,用于测试 } }
//templates/error/5XX.html <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8"> <title>Title</title> </head> <body> <h1>自定义页面 --路径:templates/error/5XX.html页面 --优先级别:2</h1> <h1>status:[[${status}]]</h1> <h2>timestamp:[[${timestamp}]]</h2> <h2>exception:[[${exception}]]</h2> <h2>myName:[[${myName}]]</h2> <h2>myAge:[[${myAge}]]</h2> <h2>extra-code:[[${extra.code}]]</h2> <h2>extra-msg:[[${extra.msg}]]</h2> </body> </html>
标签:方法 json string 求和 静态资源 image 模板 ESS lte
原文地址:https://www.cnblogs.com/wobuchifanqie/p/10153848.html