引入swagger-api 生成接口文档
1.引入pom依赖
<!-- swagger2依赖--> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger2</artifactId> <version>2.9.2</version> </dependency> <!-- swagger-ui界面依赖 --> <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-swagger-ui</artifactId> <version>2.8.0</version> </dependency>
2.增加配置类(用于指定生成API的扫描路径等)
package com.zg.common.config; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.EnableWebMvc; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurer; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; import springfox.documentation.swagger2.annotations.EnableSwagger2; @Configuration @EnableSwagger2 @EnableWebMvc public class Swagger2Config implements WebMvcConfigurer { @Bean public Docket createDocketForSwaggerAPI() { return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()).select() .apis(RequestHandlerSelectors.basePackage("com.zg.common.controller")) .paths(PathSelectors.any()) .build(); } @Bean public ApiInfo apiInfo() { return new ApiInfoBuilder() .title("zg的swagger测试接口文档") .description("测试") .termsOfServiceUrl("") .version("1.0") .build(); } @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { registry.addResourceHandler("/**").addResourceLocations("classpath:/static/"); registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/"); registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/"); WebMvcConfigurer.super.addResourceHandlers(registry); } }
@EnableWebMvc注解用于解决springboot2.6版本以后和swagger路径映射不匹配的问题
重写addResourceHandlers方法用于将swagger-ui的静态资源注册进dispecherservlet,用于解决No mapping for GET /swagger-ui.html问题
3.访问http://localhost:8080/swagger-ui.html#/接口文档
标签:springfox,documentation,api,文档,registry,org,import,swagger From: https://www.cnblogs.com/UUUz/p/18354086