这几天在做项目,从其他项目中复制粘贴拦截器的代码,然后修修改改,但是拦截器一直不起作用,请求来了进不去,最后发现是我写错了,代码如下:
配置文件:
application.yml
server:
port: 8080
servlet:
context-path: /api/v1
#springboot的配置
spring:
datasource: #定义数据源
#127.0.0.1为本机测试的ip,3306是mysql的端口号。serverTimezone是定义时区,照抄就好,mysql高版本需要定义这些东西
#useSSL也是某些高版本mysql需要问有没有用SSL连接
url: jdbc:mysql://127.0.0.1:3306/testdb?useUnicode=true&characterEncoding=utf8&useSSL=false&serverTimezone=Hongkong&useAffectedRows=true
username: root #数据库用户名,root为管理员
password: 12345678 #该数据库用户的密码
# 使用druid数据源
type: com.alibaba.druid.pool.DruidDataSource
thymeleaf:
encoding: UTF-8 #编码规范 默认
cache: false #开发阶段建议关闭缓存
prefix: classpath:/templates/
suffix: .html
mode: LEGACYHTML5 #用非严格的HTML5 默认是HTML5
servlet:
content-type: text/html
# mybatis-plus相关配置
mybatis-plus:
# xml扫描,多个目录用逗号或者分号分隔(告诉 Mapper 所对应的 XML 文件位置)
mapper-locations: classpath:mapper/*.xml
# 以下配置均有默认值,可以不设置
global-config:
db-config:
#主键类型 AUTO:"数据库ID自增" INPUT:"用户输入ID",ID_WORKER:"全局唯一ID (数字类型唯一ID)", UUID:"全局唯一ID UUID";
id-type: auto
#字段策略 IGNORED:"忽略判断" NOT_NULL:"非 NULL 判断") NOT_EMPTY:"非空判断"
field-strategy: NOT_EMPTY
#数据库类型
db-type: MYSQL
configuration:
# 是否开启自动驼峰命名规则映射:从数据库列名到Java属性驼峰命名的类似映射
map-underscore-to-camel-case: true
# 如果查询结果中包含空值的列,则 MyBatis 在映射的时候,不会映射这个字段
call-setters-on-nulls: true
# 这个配置会将执行的sql打印出来,在开发或测试的时候可以用
log-impl: org.apache.ibatis.logging.stdout.StdOutImpl
# 日志配置
logging:
group:
tomcat:
- org.apache.catalina
- org.apache.coyote
- org.apache.tomcat
# pre-defined
level:
tomcat: INFO
web: DEBUG
sql: DEBUG
WebConfig.java
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.boot.autoconfigure.condition.ConditionalOnProperty;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
/**
* 拦截器
*/
@Configuration
public class WebConfig implements WebMvcConfigurer {
@Autowired
private RequestHandlerInterceptor requestHandlerInterceptor;
@Value("${server.servlet.context-path}")
private String contextPath;
@Override
public void addInterceptors(InterceptorRegistry registry) {
registry.addInterceptor(requestHandlerInterceptor)
// 需要排除contextPath的值
.addPathPatterns(contextPath + "/**") // 即/api/v1/**
.excludePathPatterns(Consts.EXCLUDE_PATH_PATTERNS);
}
}
RequestHandlerInterceptor.java
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import org.springframework.web.servlet.HandlerInterceptor;
import org.springframework.web.servlet.ModelAndView;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.io.IOException;
import java.util.Arrays;
/**
* @Author SingleKey
* @Date 2024/7/28 8:27
*/
@Component
public class RequestHandlerInterceptor implements HandlerInterceptor {
@Override
public boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler) throws ServletException, IOException {
System.out.println("\n-------- RequestHandlerInterceptor.preHandle --- ");
return false;
}
@Override
public void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler, ModelAndView modelAndView) throws Exception {
System.out.println("\n-------- RequestHandlerInterceptor.postHandle --- ");
}
@Override
public void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler, Exception ex) throws Exception {
System.out.println("\n-------- RequestHandlerInterceptor.afterCompletion --- ");
}
}
乍一看好像没有问题,错误的地方是WebConfig.java的addPathPatterns(contextPath + "/**")这段代码,如果在配置文件中设置了context-path,那就不能再在这么写:addPathPatterns(contextPath + "/**"),不然将永远匹配不到,其实也是自己debug能力的欠缺,以及对SpringBoot的不够熟悉导致的,今日记下这个问题,供大家参考。
server:
port: 8080
servlet:
context-path: /api/v1
标签:SpringBoot2.7,18,springframework,public,拦截器,import,org,servlet,annotation
From: https://www.cnblogs.com/datom/p/18341892