Knife4j配置

一、导入依赖

  1. <!-- https://mvnrepository.com/artifact/com.github.xiaoymin/knife4j-spring-boot-starter -->
  2. <dependency>
  3. <groupId>com.github.xiaoymin</groupId>
  4. <artifactId>knife4j-spring-boot-starter</artifactId>
  5. <version>2.0.8</version>
  6. </dependency>
  7. <!-- &lt;!&ndash; 避免版本冲突 &ndash;&gt;-->
  8. <!-- <dependency>-->
  9. <!-- <groupId>com.google.guava</groupId>-->
  10. <!-- <artifactId>guava</artifactId>-->
  11. <!-- <version>29.0-jre</version>-->
  12. <!-- </dependency>-->

二、编写配置类

@Configuration
@EnableSwagger2WebMvc
//@EnableSwaggerBootstrapUI
@EnableKnife4j
public class Knife4jConfig {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("com.example.email"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        Contact contact = new Contact("zhuyijun", "http://localhost:9029/doc.html", "zhuyijun@hatech.com.cn");
        return new ApiInfoBuilder()
                .title("SpringBoot项目 后台服务API接口文档")
                .description("使用 knife4j 搭建的后台服务API接口文档")
                .termsOfServiceUrl("http://localhost:9029/")
                .contact(contact)
                .version("1.0.0")
                .build();
    }
}

三、测试

访问http://localhost:9029/doc.html