springboot集成swagger3
1 pom.xml增加
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-boot-starter</artifactId>
<version>3.0.0</version>
</dependency>
2 增加config文件
新增 Swager3Config 文件,代码如下
package com.example.app.config; import org.apache.catalina.User; import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import springfox.documentation.builders.ApiInfoBuilder; import springfox.documentation.builders.PathSelectors; import springfox.documentation.builders.RequestHandlerSelectors; import springfox.documentation.oas.annotations.EnableOpenApi; import springfox.documentation.service.ApiInfo; import springfox.documentation.spi.DocumentationType; import springfox.documentation.spring.web.plugins.Docket; @Configuration @EnableOpenApi public class Swagger3Config { @Bean public Docket api() { return new Docket(DocumentationType.OAS_30) .apiInfo(apiInfo()) //应用文档基本信息 .select() .apis(RequestHandlerSelectors.basePackage("com.example.app")) // swagger扫描路径 .paths(PathSelectors.any()) // 应用于包下所有路径 .build(); // 设置全局通用参数 } /** * 文档基本信息 */ private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("swagger口文档") .description("swagger接口文档") .version("2.0") .build(); } }
新增 MyWebMvcConfigurer文件,代码如下
package com.example.app.config; import org.springframework.context.annotation.Configuration; import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry; import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport; @Configuration public class MyWebMvcConfigurer extends WebMvcConfigurationSupport { @Override public void addResourceHandlers(ResourceHandlerRegistry registry) { // swagger配置 registry. addResourceHandler("/swagger-ui/**") .addResourceLocations("classpath:/META-INF/resources/webjars/springfox-swagger-ui/") .resourceChain(false); } }
然后在接口controller 配置就行
url 访问 ip:port/swagger-ui/index.html