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.springfox</groupId>
  <artifactId>springfox-swagger2</artifactId>
  <version>2.9.2</version>
</dependency>
<dependency>
  <groupId>io.springfox</groupId>
  <artifactId>springfox-swagger-ui</artifactId>
  <version>2.9.2</version>
</dependency>
  1. Swagger3
<dependency>
   <groupId>io.springfox</groupId>
   <artifactId>springfox-boot-starter</artifactId>
   <version>3.0.0</version>
</dependency>
  1. knife4j
<dependency>
  <groupId>com.github.xiaoymin</groupId>
  <artifactId>knife4j-spring-boot-starter</artifactId>
  <version>2.0.7</version>
</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;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值