标签:json obj ring rri ret set int 注册 容器
默认情况下,Spring Boot 提供/error
映射,以合理的方式处理所有错误,并在 servlet 容器中注册为“global”错误页面。对于机器客户端,它会生成一个 JSON 响应,其中包含错误,HTTP 状态和 exception 消息的详细信息。
可以使用上去的两个注解来处理全局异常
@RestControllerAdvice
public class GoalExceptionHandler {
@ExceptionHandler(NullPointerException.class)
public Map<String,Object> dealNullPoint(){
Map<String,Object> map = new HashMap<>(12);
map.put("stutas",1);
map.put("msg","空指针异常处理");
return map;
}
@ExceptionHandler(Exception.class)
public ModelAndView dealException(Exception e){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/public/error/error.html");
return modelAndView;
}
@Configuration
public class MyErrorViewResolver implements ErrorViewResolver {
@Override
public ModelAndView resolveErrorView(HttpServletRequest request, HttpStatus status, Map<String, Object> model) {
if (status.value()==404){
ModelAndView modelAndView = new ModelAndView();
modelAndView.setViewName("/error/404.html");
return modelAndView;
}
return null;
}
}
标签:json obj ring rri ret set int 注册 容器
原文地址:https://www.cnblogs.com/JackQiang/p/13039639.html