Spring Boot 整合Swagger2、Swagger3以及Knife4j

配置类

SwaggerConfig

@Configuration
@EnableSwagger2 //Swagger2、Knife4j
//@EnableOpenApi //Swagger3
public class SwaggerConfig {

    private ApiInfo apiInfo(){
        return new ApiInfoBuilder().title("API接口文档")
                .description("Bucks Coffee")
                .version("v1.0")
                .build();
    }

    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //- 扫描哪些包
                .apis(RequestHandlerSelectors.basePackage("com.fanle.controller"))
                //- 所有的api都显示
                .paths(PathSelectors.any())
                .build();
    }
}

pom文件

  1. Swagger2
<dependency>
  <groupId>io.springfoxgroupId>
  <artifactId>springfox-swagger2artifactId>
  <version>2.9.2version>
dependency>
<dependency>
  <groupId>io.springfoxgroupId>
  <artifactId>springfox-swagger-uiartifactId>
  <version>2.9.2version>
dependency>
  1. Swagger3
<dependency>
   <groupId>io.springfoxgroupId>
   <artifactId>springfox-boot-starterartifactId>
   <version>3.0.0version>
dependency>
  1. knife4j
<dependency>
  <groupId>com.github.xiaoymingroupId>
  <artifactId>knife4j-spring-boot-starterartifactId>
  <version>2.0.7version>
dependency>

常见问题

1.Failed to start bean ‘documentationPluginsBootstrapper’…

  • 解决方式一推荐:降低springboot的版本
  • 解决方式二:在启动类上添加@EnableWebMvc,这种方式会导致SpringBoot的@EnableAutoConfiguration失效,坑比较多。建议还是老老实实降低版本,参考博客

2.放行静态资源

接口文档页面找不到:localhost:8080/swagger-ui.htmlorlocalhost:8080/doc.html

  • swagger2
@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }
}
  • Swagger3
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
       registry.
               addResourceHandler("/swagger-ui/**")
               .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/")
               .resourceChain(false);
   }
  • knife4j
@Configuration
public class MyWebConfig implements WebMvcConfigurer {

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

进入UI界面

  • Swagger2
    localhost:8080/swagger-ui.html
  • Swagger3
    localhost:8080/swagger-ui/index.html
  • Knife4j
    localhost:8080/doc.html

swagger常用注解

@RestController
@Api(tags = "hello模块",value = "hello接口",description = "用在类上")
public class HelloController {

    @GetMapping("/hello")
    @ApiOperation("用在方法上")
    public String hello(@Valid HelloForm form){
        return form.getMsg();
    }
}
@Data
@ApiModel
public class HelloForm {
    @NotBlank
    @Pattern(regexp = "^[0-9]{6}$")
    private String msg;
}

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