一、配置文件设置 NoHandlerFoundException 异常抛出和不建立静态资源映射
spring:
mvc:
throw-exception-if-no-handler-found: true
web:
resources:
add-mappings: false
二、全局异常中增加 NoHandlerFoundException 的捕获处理
@Slf4j
@RestControllerAdvice
public class GlobalExceptionHandler {
/**
* NoHandlerFoundException 异常捕获处理
* 请求的接口不存在
*/
@ExceptionHandler(NoHandlerFoundException.class)
public <T> Response<T> noHandlerFoundExceptionHandler(HttpServletRequest request, NoHandlerFoundException e) {
log.error("BAD_REQUEST|{}|{}: {}|{}", null != request ? request.getRequestURI() : "", e.getClass().getSimpleName(), e.getMessage(), null != request ? HttpClientUtil.getIpAddress() : "");
return Response.warn(StatusCode.REQUEST_NOT_ALLOWED);
}
}