关于在springboot3.xx使用knif4j及其拦截器的拦截方法

原因在于使用新版本的knif4j版本后,以前的方法,拦截器的配置,配置文件,配置属性发生了巨大的变化,如果你也有这样的困扰,希望这篇可以帮助到你

如何在springboot3.xx版本使用knif4j我以及写过,这里便不在赘述了,我主要写拦截器的配置

这里时传送门http://t.csdnimg.cn/tFDS8

如果我们统一配置了拦截器不去配置的话,我们一般情况下会出现No mapping for GET /doc.html的错误,这种情况出现的原因是项目中含有继承WebMvcConfigurationSupport的类,这会导致 swagger 配置失效。

解决方法:重写WebMvcConfiguration的方法

@Override
public void addResourceHandlers(ResourceHandlerRegistry registry) {
    registry.addResourceHandler("doc.html")
            .addResourceLocations("classpath:/META-INF/resources/");

    registry.addResourceHandler("swagger-ui.html")
            .addResourceLocations("classpath:/META-INF/resources/");
    registry.addResourceHandler("/webjars/**")
            .addResourceLocations("classpath:/META-INF/resources/webjars/");
}

多说一句:我在解决这个问题是,看到网上都是在注册拦截器是排除该方法,类似于以下的书写,这并没有帮助我解决问题,不知道有没有大佬知道是为什么

public class WebMVCConfiguration  extends WebMvcConfigurationSupport {


    @Autowired
    private JwtTokenUserInterceptor jwtTokenUserInterceptor;
    protected void addInterceptors(InterceptorRegistry registry) {
        log.info("开始注册自定义拦截器...");
        registry.addInterceptor(jwtTokenUserInterceptor)
                .addPathPatterns("/user/**")
                .excludePathPatterns("/user/login")
                .excludePathPatterns("/admin/register")
                .excludePathPatterns("/static/**,/swagger-resources,/**webjars/**,/v3/**");



    }
  • 18
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Sure! To integrate Spring Boot with Knife4j, you can follow these steps: 1. Add the required dependencies to your project's `pom.xml` file: ```xml <dependency> <groupId>com.github.xiaoymin</groupId> <artifactId>knife4j-spring-boot-starter</artifactId> <version>2.0.2</version> </dependency> ``` 2. Create a new configuration class, typically named `SwaggerConfiguration`, and annotate it with `@Configuration`: ```java @Configuration public class SwaggerConfiguration { } ``` 3. Inside the `SwaggerConfiguration` class, add the necessary bean definitions for `Docket` and `ApiInfo`: ```java @Configuration public class SwaggerConfiguration { @Bean public Docket api() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.example.controller")) // Set your controller package .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Your API Documentation") .description("Description of your API") .version("1.0.0") .build(); } } ``` 4. Make sure to update the `basePackage` value in the `RequestHandlerSelectors` to match the package where your Spring MVC controllers are located. 5. Run your Spring Boot application, and you should be able to access the Knife4j swagger UI at `http://localhost:8080/doc.html`, where `8080` is your application's port. That's it! You have now integrated Knife4j with your Spring Boot application. You can customize the Swagger configuration further according to your requirements.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值