SpringBoot3整合Knife4J,以及被拦截无法显示doc.html问题

引入坐标:

    
      com.github.xiaoymin
      knife4j-openapi3-jakarta-spring-boot-starter
      4.4.0
    

  

application.yml:

# springdoc-openapi项目配置
springdoc:
  swagger-ui:
    path: /swagger-ui.html
    tags-sorter: alpha
    operations-sorter: alpha
  api-docs:
    path: /v3/api-docs
  group-configs:
    - group: 'default'
      paths-to-match: '/**'
      packages-to-scan: xxx.xxx.controller   #改成你自己的controller包
# knife4j的增强配置,不需要增强可以不配
knife4j:
  enable: true
  setting:
    language: zh_cn

一般来说以上的配置就行了。

但是,如果你配置了拦截器则需要放行,不然访问不了doc.html:

@Configuration
@Slf4j
public class WebConfig implements WebMvcConfigurer {
    @Autowired
    private LoginInterceptor loginInterceptor;

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 登陆和注册不拦截
        registry.addInterceptor(loginInterceptor).excludePathPatterns("/login","/register","/static/**",
                "/webjars/**","/doc.html","/v3/**"  // knife4j相关放行
        );
    }

最后访问网址:

http://localhost:8080/doc.html

你可能感兴趣的:(Java,java,spring,boot)