diff --git a/server/like-admin/src/main/resources/application.yml b/server/like-admin/src/main/resources/application.yml index 9781008f..d5f0e916 100644 --- a/server/like-admin/src/main/resources/application.yml +++ b/server/like-admin/src/main/resources/application.yml @@ -12,6 +12,10 @@ spring: active: dev mvc: static-path-pattern: /api/static/** + throw-exception-if-no-handler-found: true + web: + resources: + add-mappings: false # 数据源配置 datasource: url: jdbc:mysql://localhost:3306/likeadmin?useUnicode=true&characterEncoding=UTF-8&autoReconnect=true&useSSL=false diff --git a/server/like-common/src/main/java/com/mdd/common/exception/GlobalException.java b/server/like-common/src/main/java/com/mdd/common/exception/GlobalException.java index af83f87b..2b7602ba 100644 --- a/server/like-common/src/main/java/com/mdd/common/exception/GlobalException.java +++ b/server/like-common/src/main/java/com/mdd/common/exception/GlobalException.java @@ -13,6 +13,7 @@ import org.springframework.web.HttpRequestMethodNotSupportedException; import org.springframework.web.bind.MethodArgumentNotValidException; import org.springframework.web.bind.MissingServletRequestParameterException; import org.springframework.web.bind.annotation.*; +import org.springframework.web.servlet.NoHandlerFoundException; import java.util.Objects; @@ -38,6 +39,16 @@ public class GlobalException { return AjaxResult.failed(HttpEnum.SYSTEM_ERROR.getCode(), e.getMessage()); } + /** + * 拦截404异常 + */ + @ResponseStatus(HttpStatus.OK) + @ExceptionHandler(NoHandlerFoundException.class) + @ResponseBody + public AjaxResult handleNoHandlerFoundException(NoHandlerFoundException e){ + return AjaxResult.failed(HttpEnum.REQUEST_404_ERROR.getCode(), e.getMessage()); + } + /** * 拦截自定义抛出异常 */ diff --git a/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java b/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java index 252a700a..e89c7b08 100644 --- a/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java +++ b/server/like-front/src/main/java/com/mdd/front/LikeFrontInterceptor.java @@ -31,16 +31,11 @@ public class LikeFrontInterceptor implements HandlerInterceptor { UserMapper userMapper; @Override - public boolean preHandle(@NonNull HttpServletRequest request, HttpServletResponse response, @NonNull Object handler) throws Exception { - // 404拦截 - response.setContentType("application/json;charset=utf-8"); - if (response.getStatus() == 404) { - AjaxResult result = AjaxResult.failed(HttpEnum.REQUEST_404_ERROR.getCode(), HttpEnum.REQUEST_404_ERROR.getMsg()); - response.getWriter().print(JSON.toJSONString(result)); - return false; - } - + public boolean preHandle(@NonNull HttpServletRequest request, + @NonNull HttpServletResponse response, + @NonNull Object handler) throws Exception { // 判断请求接口 + response.setContentType("application/json;charset=utf-8"); if (!(handler instanceof HandlerMethod)) { return HandlerInterceptor.super.preHandle(request, response, handler); } @@ -113,7 +108,9 @@ public class LikeFrontInterceptor implements HandlerInterceptor { } @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(); HandlerInterceptor.super.afterCompletion(request, response, handler, ex); } diff --git a/server/like-front/src/main/resources/application.yml b/server/like-front/src/main/resources/application.yml index 377ea3a4..ac2218ea 100644 --- a/server/like-front/src/main/resources/application.yml +++ b/server/like-front/src/main/resources/application.yml @@ -10,6 +10,10 @@ spring: active: dev mvc: static-path-pattern: /api/static/** + throw-exception-if-no-handler-found: true + web: + resources: + add-mappings: false # 数据源配置 datasource: 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: - mapper-locations: classpath*:/mapper/**Mapper.xml # 映射文件路径 + mapper-locations: classpath*:/mapper/**Mapper.xml typeAliasesPackage: com.mdd.**.mapper global-config: banner: false db-config: - table-prefix: la_ # 设置表前缀 + table-prefix: la_ configuration-properties: - prefix: la_ # 自定义表前缀标签${prefix} \ No newline at end of file + prefix: la_ \ No newline at end of file