实现方案
在请求control时加一个前置拦截器,在preHandle中得到HandlerMethod,判断请求的Controller的method的返回类型,
以及请求的Controller方法是否使用的@RestController或者@ResponBody注解。若满足以上条件则抛出异常时,
应该以json格式的报错信息,否则返回错误页面。
写一个拦截器类
/**
* 判断Controller的返回类型,是json还是view
*/
@Component
public class MyExceptionPreHandleInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws Exception {
if (!(handler instanceof HandlerMethod)) {
return true;
}
//默认设置为页面返回模式
request.setAttribute("my_return_is_view", true);
if(hm.getBeanType().isAnnotationPresent(RestController.class)) {
//有RestController的类注解的类里面的method一定是返回json
request.setAttribute("my_return_is_view", false);
}else{
Method method = hm.getMethod();
if (method.isAnnotationPresent(ResponseBody.class)) {
//有ResponseBody的类注解的方法一定是返回json
request.setAttribute("my_return_is_view", false);
}
}
return true;
}
}