其他分享
首页 > 其他分享> > swagger3 配置

swagger3 配置

作者:互联网

在pom.xml添加swagger3.0的依赖:

<dependency>
       <groupId>io.springfox</groupId>
       <artifactId>springfox-boot-starter</artifactId>
       <version>3.0.0</version>
</dependency>

创建SwaggerConfig 配置类

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.WebMvcConfigurationSupport;
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;

@Configuration
@EnableWebMvc
public class SwaggerConfig extends WebMvcConfigurationSupport {

    @Bean
    public Docket docketCategory() {
        return new Docket(DocumentationType.OAS_30)
                .apiInfo(apiInfo())
                // 分组名称
                .groupName("default")
                //使能swagger
                .enable(true)
                .select()
                //设置接口包名
                .apis(RequestHandlerSelectors.basePackage("com.example.controler"))
                //设置请求路径开头的接口
                .paths(PathSelectors.ant("/files/**"))
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("接口总览")
                .description("测试")
                .version("1.0")
                .build();
    }
    @Override
    protected void addResourceHandlers(ResourceHandlerRegistry registry) {
        // 解决静态资源无法访问
        registry.addResourceHandler("/**").addResourceLocations("classpath:/static/");
        // 解决swagger无法访问
        registry.addResourceHandler("/swagger-ui.html").addResourceLocations("classpath:/META-INF/resources/");
        // 解决swagger的js文件无法访问
        registry.addResourceHandler("/webjars/**").addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}

 访问:http://localhost:8080/swagger-ui/index.html

标签:配置,documentation,springframework,swagger3,org,import,springfox,annotation
来源: https://www.cnblogs.com/cokelike/p/16630355.html