SpringBoot项目使用knife4j实现自动化接口文档

SpringBoot项目使用knife4j实现自动化接口文档,knife4j 的swagger-bootstrap-ui 是springfox-swagger的增强UI实现,以下为实现步骤:

1. pom 依赖配置

下面提供两种风格

风格一:


 io.springfox
 springfox-swagger2
 2.9.2



  com.github.xiaoymin
  swagger-bootstrap-ui
  1.9.6

风格二:



 io.springfox
 springfox-swagger2
 2.9.2



    com.github.xiaoymin
    knife4j-spring-ui
    2.0.4


    com.github.xiaoymin
    knife4j-spring-boot-starter
    2.0.4

2. SwaggerConfiguration配置


@Configuration
@EnableSwagger2
public class SwaggerConfiguration {

 @Bean
 public Docket createRestApi() {
     return new Docket(DocumentationType.SWAGGER_2)
     .apiInfo(apiInfo())
     .select()
     .apis(RequestHandlerSelectors.basePackage("com.test.module.app.controller"))
     .paths(PathSelectors.any())
     .build();
 }

 private ApiInfo apiInfo() {
     return new ApiInfoBuilder()
     .title("swagger-bootstrap-ui RESTful APIs")
     .description("swagger-bootstrap-ui")
     .termsOfServiceUrl("http://localhost:8999/")
     .contact("[email protected]")
     .version("1.0")
     .build();
 }
}

3. WebMvcConfigurer配置

@Configuration
public class MyWebAppConfigurer  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/");
    }

}

4. 访问地址

访问地址是:http://${host}:${port}/doc.html

你可能感兴趣的:(JAVA,Spring,java,api最新7.0)