如何定制错误页面?
(1)在有模板引擎的情况下:在templates文件夹下的error/状态码;即将错误页面命名为:错误状态码.html放在templates文件夹里面的error文件夹下,发生此状态码的错误会来到对应的页面。
页面可以获得的信息:
timestamp:时间
status:状态码
error:错误提示
exception:异常对象
message:异常消息
errors:JSR303数据校验的错误都在这里
(2)如果没有模板引擎,就在静态资源文件static下的error文件夹下找。
(3)如果都没有,则返回系统的默认错误页面。
在错误页面可以这么获取到数据:
<h1>status:[[${status}]]</h1>
<h2>timestamp:[[${timestamp}]]</h2>
如何定制错误的json数据?
首先我们在com.gong.springbootcurd下新建一个exception文件夹,该文件夹中定义一个异常:UserNoExistException.java
package com.gong.springbootcurd.exception;
public class UserNotExistException extends RuntimeException {
public UserNotExistException() {
super("用户不存在");
}
}
然后在com.gong.springbootcurd.component下新建一个异常处理器:MyExceptionHandler.java
package com.gong.springbootcurd.component;
import com.gong.springbootcurd.exception.UserNotExistException;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.Map;
@ControllerAdvice
public class MyExceptionHandler {
//1、浏览器客户端返回的都是json
@ResponseBody
@ExceptionHandler(UserNotExistException.class)
public Map<String,Object> handleException(Exception e){
Map<String,Object> map = new HashMap<>();
map.put("code","user.notexist");
map.put("message",e.getMessage());
return map;
}
}
在com.gong.springbootcurd.controller的HelloController.java中添加:
@ResponseBody
@RequestMapping("/hello")
public String hello(@RequestParam("user") String user){
if(!user.equals("aaa")){
throw new UserNotExistException();
}
return "hello world";
}
我们在服务器种模拟请求:
会显示我们自定的json错误信息。
如何设置自适应的显示错误页面?
也就是说浏览器显示的就是错误页面,而客户端显示的是json的错误信息。
修改MyExceptionHandler.java里面为:
@ExceptionHandler(UserNotExistException.class)
public String handleException(Exception e, HttpServletRequest request){
Map<String,Object> 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中可以这么获取信息了:
<h1>status:[[${status}]]</h1>
<h2>timestamp:[[${timestamp}]]</h2>
<h2>exception:[[${exception}]]</h2>
<h2>message:[[${message}]]</h2>
<h2>ext:[[${ext.code}]]</h2>
<h2>ext:[[${ext.message}]]</h2>
当访问http://localhost:8080/curd/hello?user=aa时,会看到:
这里exception获取不到???暂时还不知道什么原因。
如何定制自己的错误信息到页面中?
向上述的ext.code和 ext.message是我们异常处理器给我们带的字段,如果我们想新增自己的字段:
在com.gong.springbootcurd.component中新建一个MyErrorAttributes.java
package com.gong.springbootcurd.component;
import com.gong.springbootcurd.exception.UserNotExistException;
import org.springframework.boot.web.servlet.error.DefaultErrorAttributes;
import org.springframework.stereotype.Component;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.WebRequest;
import java.util.Map;
//给容器中加入我们自己定义的ErrorAttributes
@Component
public class MyErrorAttributes extends DefaultErrorAttributes {
//返回值的map就是页面和json能获取的所有字段
public Map<String, Object> getErrorAttributes(WebRequest requestAttributes, boolean includeStackTrace) {
Map<String, Object> map = super.getErrorAttributes(requestAttributes, includeStackTrace);
//map.put("exception", UserNotExistException.class.getName());
map.put("company","gong");
//我们的异常处理器携带的数据
Map<String,Object> ext = (Map<String, Object>) requestAttributes.getAttribute("ext", 0);
map.put("ext",ext);
return map;
}
}
说明:我们先从请求域中得到与系统默认的错误相关的属性,然后再添加自己定义的属性,最后从请求域中得到自定义异常处理器中的属性,全部都传给map进行返回。对于没有打印出来的exception,我们可以这么进行处理,在自定义的异常处理器中:
map.put("exception",e.getClass().getName());
我们自己来获得异常的名字,然后传给exception,只不过最后我们在5xx.html中这么取值:
<h1>status:[[${status}]]</h1>
<h2>timestamp:[[${timestamp}]]</h2>
<h2>exception:[[${exception}]]</h2>
<h2>error:[[${error}]]</h2>
<h2>message:[[${message}]]</h2>
<h2>company:[[${company}]]</h2>
<h2>ext:[[${ext.code}]]</h2>
<h2>ext:[[${ext.message}]]</h2>
<h2>ext:[[${ext.exception}]]</h2>
最后在浏览器输入:loclahost:8080/curd/hello?user=aa