首页  编辑  

Java/SpringBoot集成Swagger

Tags: /Java/   Date Created:
1. 添加 pom 依赖:
  1.         <dependency>
  2.             <groupId>io.springfox</groupId>
  3.             <artifactId>springfox-swagger2</artifactId>
  4.             <version>3.0.0</version>
  5.         </dependency>
  6.         <dependency>
  7.             <groupId>io.springfox</groupId>
  8.             <artifactId>springfox-swagger-ui</artifactId>
  9.             <version>3.0.0</version>
  10.         </dependency>
  11.         <dependency>
  12.             <groupId>io.springfox</groupId>
  13.             <artifactId>springfox-boot-starter</artifactId>
  14.             <version>3.0.0</version>
  15.         </dependency>
2. Swagger 配置:
  1. @Configuration
  2. @EnableSwagger2
  3. public class SwaggerConfig implements WebMvcConfigurer {
  4.     @Bean
  5.     public Docket createRestApi() {
  6.         ApiInfo apiInfo =
  7.                 new ApiInfoBuilder()
  8.                         .title("API")
  9.                         .description("Tacos API")
  10.                         .version("1.0")
  11.                         .contact(new Contact("Admin""https://github.com/yutils""3373217@qq.com"))
  12.                         .license("我的首页")
  13.                         .licenseUrl("https://weibo.com/32005200")
  14.                         .build();
  15.         return new Docket(DocumentationType.SWAGGER_2)
  16.                 .apiInfo(apiInfo)
  17.                 .select()
  18.                 .apis(RequestHandlerSelectors.basePackage("tacos.controller"))
  19.                 .paths(PathSelectors.any())
  20.                 .build();
  21.     }
  22.     @Override
  23.     public void addResourceHandlers(ResourceHandlerRegistry registry) {
  24.         registry.
  25.                 addResourceHandler("/swagger-ui/**")
  26.                 .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
  27.                 .resourceChain(false);
  28.     }
  29.     @Override
  30.     public void addViewControllers(ViewControllerRegistry registry) {
  31.         registry.addViewController("/swagger-ui/")
  32.                 .setViewName("forward:" + "/swagger-ui/index.html");
  33.     }
  34. }
3. 排障:
org.springframework.context.ApplicationContextException: Failed to start bean 'documentationPluginsBootstrapper'; nested exception is java.lang.NullPointerException:Cannot invoke "org.springframework.web.servlet.mvc.condition.PatternsRequestCondition.toString()" because the return value of "springfox.documentation.spi.service.contexts.Orderings.patternsCondition(springfox.documentation.RequestHandler)" is null
这个问题是Spring Boot 2.6及以上版本,确实将路径匹配规则的默认实现从AntPathMatcher更改为PathPatternMatcher。这可能导致在使用一些旧版本的代码或配置时出现问题。
如果你想在Spring Boot 2.6+版本中恢复旧的路径匹配规则(即使用AntPathMatcher),可以通过配置spring.mvc.pathmatch.matching-strategy属性来解决问题。
在application.properties文件中,添加以下配置:
spring.mvc.pathmatch.matching-strategy=ant_path_matcher
在application.yml文件中,添加以下配置:
spring: 
    mvc: 
        pathmatch: 
            matching-strategy: ant_path_matcher
通过将spring.mvc.pathmatch.matching-strategy属性设置为ant_path_matcher,你可以告诉Spring Boot使用旧的AntPathMatcher作为路径匹配策略。
这样做将恢复与之前版本相同的路径匹配行为,并解决由此更改引起的问题。请确保在升级到新版本时,检查和更新代码和配置,以适应新的路径匹配规则。