怎样用springboot开发cs_springboot开发之配置自定义的错误界面和错误信息
如何定制错误页面?
(1)在有模板引擎的情况下:在template文件夹下的error/状态码;即将错误页面命名为:错误状态码.html放在template文件夹里面的error文件夹下,发生此状态码的错误会来到对应的页面。
页面可以获得的信息:
timestamp:shiajianc
status:状态码
error:错误提示
exception:异常对象
message:异常消息
errors:JSR303数据校验的错误都在这里
(2)如果没有模板引擎,就在静态资源文件static下的error文件夹下找。
(3)如果都没有,则返回系统的默认错误页面。
在错误页面可以这么获取到数据:
status:[[${status}]]
timestamp:[[${timestamp}]]
如何定制错误的json数据?
首先我们在com.gong.springbootcurd下新建一个exception文件夹,该文件夹中定义一个异常:UserNoExistException.java
packagecom.gong.springbootcurd.exception;public class UserNotExistException extendsRuntimeException {publicUserNotExistException() {super("用户不存在");
}
}
然后在com.gong.springbootcurd.component下新建一个异常处理器:MyExceptionHandler.java
packagecom.gong.springbootcurd.component;importcom.gong.springbootcurd.exception.UserNotExistException;importorg.springframework.web.bind.annotation.ControllerAdvice;importorg.springframework.web.bind.annotation.ExceptionHandler;importorg.springframework.web.bind.annotation.ResponseBody;importjavax.servlet.http.HttpServletRequest;importjava.util.HashMap;importjava.util.Map;
@ControllerAdvicepublic classMyExceptionHandler {//1、浏览器客户端返回的都是json
@ResponseBody
@ExceptionHandler(UserNotExistException.class)public MaphandleException(Exception e){
Map map = new HashMap<>();
map.put("code","user.notexist");
map.put("message",e.getMessage());returnmap;
}
}
在com.gong.springbootcurd.controller的HelloController.java中添加:
@ResponseBody
@RequestMapping("/hello")public String hello(@RequestParam("user") String user){if(!user.equals("aaa")){throw newUserNotExistException();
}return "hello world";
}
我们在服务器种模拟请求:
会显示我们自定的json错误信息。
如何设置自适应的显示错误页面?
也就是说浏览器显示的就是错误页面,而客户端显示的是json的错误信息。
修改MyExceptionHandler.java里面为:
@ExceptionHandler(UserNotExistException.class)publicString handleException(Exception e, HttpServletRequest request){
Map map = new HashMap<>();//传入我们自己的错误状态码 4xx 5xx
/*** Integer statusCode = (Integer) request
.getAttribute("javax.servlet.error.status_code");*/request.setAttribute("javax.servlet.error.status_code",500);
map.put("code","user.notexist");
map.put("message","用户出错啦");
request.setAttribute("ext",map);//转发到/error
return "forward:/error";
}
需要注意的是我们必须要设置响应的状态码,最后转发到错误error请求。
在自己定义的5xx.html中可以这么获取信息了:
status:[[${status}]]
timestamp:[[${timestamp}]]
exception:[[${exception}]]
message:[[${message}]]
ext:[[${ext.code}]]
ext:[[${ext.message}]]
当访问http://localhost:8080/curd/hello?user=aa时,会看到:
这里exception获取不到???暂时还不知道什么原因。
如何定制自己的错误信息到页面中?
向上述的ext.code和 ext.message是我们异常处理器给我们带的字段,如果我们想新增自己的字段:
在com.gong.springbootcurd.component中新建一个MyErrorAttributes.java
packagecom.gong.springbootcurd.component;importcom.gong.springbootcurd.exception.UserNotExistException;importorg.springframework.boot.web.servlet.error.DefaultErrorAttributes;importorg.springframework.stereotype.Component;importorg.springframework.web.context.request.RequestAttributes;importorg.springframework.web.context.request.WebRequest;importjava.util.Map;//给容器中加入我们自己定义的ErrorAttributes
@Componentpublic class MyErrorAttributes extendsDefaultErrorAttributes{//返回值的map就是页面和json能获取的所有字段
public Map getErrorAttributes(WebRequestrequestAttributes, booleanincludeStackTrace) {
Map map = super.getErrorAttributes(requestAttributes, includeStackTrace);//map.put("exception", UserNotExistException.class.getName());
map.put("company","gong");//我们的异常处理器携带的数据
Map ext = (Map) requestAttributes.getAttribute("ext", 0);
map.put("ext",ext);returnmap;
}
}
说明:我们先从请求域中得到与系统默认的错误相关的属性,然后再添加自己定义的属性,最后从请求域中得到自定义异常处理器中的属性,全部都传给map进行返回。对于没有打印出来的exception,我们可以这么进行处理,在自定义的异常处理器中:
map.put("exception",e.getClass().getName());
我们自己来获得异常的名字,然后传给exception,只不过最后我们在5xx.html中这么取值:
status:[[${status}]]
timestamp:[[${timestamp}]]
exception:[[${exception}]]
error:[[${error}]]
message:[[${message}]]
company:[[${company}]]
ext:[[${ext.code}]]
ext:[[${ext.message}]]
ext:[[${ext.exception}]]
最后在浏览器输入:loclahost:8080/curd/hello?user=aa
总结
以上是生活随笔为你收集整理的怎样用springboot开发cs_springboot开发之配置自定义的错误界面和错误信息的全部内容,希望文章能够帮你解决所遇到的问题。
- 上一篇: 范例ppt_消防安全知识主题班会PPT,
- 下一篇: 现代控制理论输出y_现代控制理论试题及答