优化404拦截写法

This commit is contained in:
TinyAnts 2022-11-16 23:21:59 +08:00
parent dafaf09011
commit e211426c1a
4 changed files with 29 additions and 13 deletions

View File

@ -12,6 +12,10 @@ spring:
active: dev active: dev
mvc: mvc:
static-path-pattern: /api/static/** static-path-pattern: /api/static/**
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false
# 数据源配置 # 数据源配置
datasource: datasource:
url: jdbc:mysql://localhost:3306/likeadmin?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false url: jdbc:mysql://localhost:3306/likeadmin?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false

View File

@ -13,6 +13,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException;
import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.MissingServletRequestParameterException;
import org.springframework.web.bind.annotation.*; import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.NoHandlerFoundException;
import java.util.Objects; import java.util.Objects;
@ -38,6 +39,16 @@ public class GlobalException {
return AjaxResult.failed(HttpEnum.SYSTEM_ERROR.getCode(), e.getMessage()); return AjaxResult.failed(HttpEnum.SYSTEM_ERROR.getCode(), e.getMessage());
} }
/**
* 拦截404异常
*/
@ResponseStatus(HttpStatus.OK)
@ExceptionHandler(NoHandlerFoundException.class)
@ResponseBody
public AjaxResult<Object> handleNoHandlerFoundException(NoHandlerFoundException e){
return AjaxResult.failed(HttpEnum.REQUEST_404_ERROR.getCode(), e.getMessage());
}
/** /**
* 拦截自定义抛出异常 * 拦截自定义抛出异常
*/ */

View File

@ -31,16 +31,11 @@ public class LikeFrontInterceptor implements HandlerInterceptor {
UserMapper userMapper; UserMapper userMapper;
@Override @Override
public boolean preHandle(@NonNull HttpServletRequest request, HttpServletResponse response, @NonNull Object handler) throws Exception { public boolean preHandle(@NonNull HttpServletRequest request,
// 404拦截 @NonNull HttpServletResponse response,
response.setContentType("application/json;charset=utf-8"); @NonNull Object handler) throws Exception {
if (response.getStatus() == 404) {
AjaxResult<Object> result = AjaxResult.failed(HttpEnum.REQUEST_404_ERROR.getCode(), HttpEnum.REQUEST_404_ERROR.getMsg());
response.getWriter().print(JSON.toJSONString(result));
return false;
}
// 判断请求接口 // 判断请求接口
response.setContentType("application/json;charset=utf-8");
if (!(handler instanceof HandlerMethod)) { if (!(handler instanceof HandlerMethod)) {
return HandlerInterceptor.super.preHandle(request, response, handler); return HandlerInterceptor.super.preHandle(request, response, handler);
} }
@ -113,7 +108,9 @@ public class LikeFrontInterceptor implements HandlerInterceptor {
} }
@Override @Override
public void afterCompletion(@NonNull HttpServletRequest request, @NonNull HttpServletResponse response, @NonNull Object handler, Exception ex) throws Exception { public void afterCompletion(@NonNull HttpServletRequest request,
@NonNull HttpServletResponse response,
@NonNull Object handler, Exception ex) throws Exception {
LikeFrontThreadLocal.remove(); LikeFrontThreadLocal.remove();
HandlerInterceptor.super.afterCompletion(request, response, handler, ex); HandlerInterceptor.super.afterCompletion(request, response, handler, ex);
} }

View File

@ -10,6 +10,10 @@ spring:
active: dev active: dev
mvc: mvc:
static-path-pattern: /api/static/** static-path-pattern: /api/static/**
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false
# 数据源配置 # 数据源配置
datasource: datasource:
url: jdbc:mysql://localhost:3306/local_likeadmin?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false url: jdbc:mysql://localhost:3306/local_likeadmin?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false
@ -48,11 +52,11 @@ spring:
# Mybatis-plus配置 # Mybatis-plus配置
mybatis-plus: mybatis-plus:
mapper-locations: classpath*:/mapper/**Mapper.xml # 映射文件路径 mapper-locations: classpath*:/mapper/**Mapper.xml
typeAliasesPackage: com.mdd.**.mapper typeAliasesPackage: com.mdd.**.mapper
global-config: global-config:
banner: false banner: false
db-config: db-config:
table-prefix: la_ # 设置表前缀 table-prefix: la_
configuration-properties: configuration-properties:
prefix: la_ # 自定义表前缀标签${prefix} prefix: la_