springboot异常处理
1 、错误处理
默认规则
默认情况下,Spring Boot提供/error处理所有错误的映射
对于机器客户端,它将生成JSON响应,其中包含错误,HTTP状态和异常消息的详细信息。
对于浏览器客户端,响应一个“ whitelabel”错误视图,以HTML格式呈现相同的数据
error/下的4xx,5xx页面会被自动解析;
2、自定义错误信息
@Component //@component (把普通pojo实例化到spring容器中,相当于配置文件中的
public class MyErrorAttribute extends DefaultErrorAttributes {
// @Override
// public Map<String, Object> getErrorAttributes(WebRequest webRequest, boolean includeStackTrace) {
// Map<String,Object> map=super.getErrorAttributes(webRequest,includeStackTrace);
// map.put("myerror","errormy");
// return map;
// }
@Override
public Map<String, Object> getErrorAttributes(WebRequest webRequest, ErrorAttributeOptions options) {
Map<String,Object> map=super.getErrorAttributes(webRequest, options);
map.put("myerror","errormy");
return map;
}
}