一.当项目没有静态资源时,全局请求都统一返回json。
可以采用如下配置:
1. springmvc.xml 添加配置
<!-- 控制器增强,使一个Contoller成为全局的异常处理类,类中用@ExceptionHandler方法注解的方法可以处理所有Controller发生的异常 -->
<context:include-filter type="annotation" expression="org.springframework.web.bind.annotation.ControllerAdvice" />
2.创建 控制器增强 类
@ControllerAdvice 这个注解可以参考
https://www.iteye.com/blog/gsshijun-2321980
https://my.oschina.net/zhangxufeng/blog/2222434
https://www.cnblogs.com/lenve/p/10748453.html
@ControllerAdvice // 控制器增强
@ResponseBody
public class ExceptionAspect{
private static final Logger log =LoggerFactory.getLogger(ExceptionAspect.class);
@ExceptionHandler(value = Exception.class)
public Response defaultErrorHandler(HttpServletRequest request, HttpServletResponse response, Exception e) throws Exception {
String url = request.getServletPath();
String message = e.getMessage();
String simpleName = e.getClass().getSimpleName();
log.error("|url={}|simpleName={}|message={}",url,simpleName,message);
log.error("This Exception is {}",e);
if (e instanceof MissingServletRequestParameterException) {
return new Response().failure("400",message);
}
if (e instanceof HttpMessageNotReadableException) {
return new Response().failure("400",message);
}
if (e instanceof MethodArgumentNotValidException) { //参数验证异常...
return new Response().failure("400",message);
}
if (e instanceof NoHandlerFoundException) {
return new Response().failure("404","NoHandlerFound");
}
if (e instanceof NoSuchRequestHandlingMethodException) {
return new Response().failure("404","NoHandlerFound");
}
if (e instanceof HttpRequestMethodNotSupportedException) {
return new Response().failure("405",message);
}
if (e instanceof HttpMediaTypeNotAcceptableException) {
return new Response().failure("406",message);
}
if (e instanceof HttpMediaTypeNotSupportedException) { //内容类型不支持...
return new Response().failure("415",message);
}
return new Response().failure("500",message);
}
}
Response 是我自己写的 返回实体类。
3. web.xml 添加配置
<servlet>
<servlet-name>SpringMVC</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath:springmvc.xml</param-value>
</init-param>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
这个配置是为了拦截 404 请求,因为DispatcherServlet.java中这个属性的默认值就是false,throwExceptionIfNoHandlerFound为false,不会抛出异常,而是会给浏览器一个404状态的响应。
二.当项目有静态资源时,全局请求都统一返回json。
当项目有静态资源时,习惯性在配置文件中添加 <mvc:default-servlet-handler /> 解释:使用默认的Servlet来响应静态文件 容器默认的DefaultServletHandler处理 所有静态内容与无RequestMapping处理的URL。
配置文件有这个 属性时,会使web.xml 配置的 :
<init-param>
<param-name>throwExceptionIfNoHandlerFound</param-name>
<param-value>true</param-value>
</init-param>
失效。
这时就不会拦截 404 请求。
为了静态资源和404请求通知满足,先去掉<mvc:default-servlet-handler /> ,加上<mvc:resources mapping="/content/**" location="/content/"/> 这个标签。这个标签其实就是一个 RequestMapping ,正则 定向 的指向静态资源。而<mvc:default-servlet-handler /> 是除了RequestMapping 路径所有的路径都去找静态资源,相当于 @RequestMapping(“/**”)。
有几篇文章可以参考下
https://www.jianshu.com/p/623a17bf85f2
https://www.jianshu.com/p/623a17bf85f2
https://blog.csdn.net/butioy_org/article/details/78718405