后端API接口文档Swagger使用

一款致力于解决接口规范化,标准化,文档化的开源库
一款可以根据restful风格生成的接口开发文档,并且支持做测试的一款中间软件

SpringBoot项目嵌入Swagger

引入依赖

		<dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger2artifactId>
            <version>3.0.0version>
        dependency>
        <dependency>
            <groupId>io.springfoxgroupId>
            <artifactId>springfox-swagger-uiartifactId>
            <version>3.0.0version>
        dependency>

创建配置Swagger配置类

@EnableSwagger2
@Configuration
public class SwaggerConfig {

    //配置Swagger2的Bean实例
    @Bean
    public Docket docket(){
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                //是否启用Swagger
                .enable(true)
                .select()
                //RequestHandlerSelectors配置要扫描接口的方式
                //basePackage指定要扫描的包
                //any()扫描全部
                //none()不扫描
                //withClassAnnotation:扫描类上的注解
                //withMethodAnnotation:扫描方法上的注解
                .apis(RequestHandlerSelectors.basePackage("com.bigcat.admin.cnotroller"))
                //path()过滤路径
                .paths(PathSelectors.ant("/bigcat/**"))
                .build();
    }

    //配置Swagger信息 apiInfo
    private ApiInfo apiInfo(){
        return new ApiInfo("Api Documentation标题",
                "Api Documentation描述",
                "1.0",
                "https://blog.kuangstudy.com/",
                new Contact("YiMing","https://blog.kuangstudy.com","[email protected]"),
                "Apache 2.0",
                "https://www.apache.org/licenses/LICENSE-2.0",
                new ArrayList());
    }
}

关于高版本SpringBoot整合Swagger2出现Error的解决方案

方案一:

spring:
	mvc:
    	pathmatch:
      		matching-strategy: ANT_PATH_MATCHER

方案二:

配置WebMvcConfigure.java

@Configuration
public class WebMvcConfigurer extends WebMvcConfigurationSupport {

    /**
     * 发现如果继承了WebMvcConfigurationSupport,则在yml中配置的相关内容会失效。 需要重新指定静态资源
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("/**").addResourceLocations(
                "classpath:/static/");
        registry.addResourceHandler("swagger-ui.html", "doc.html").addResourceLocations(
                "classpath:/META-INF/resources/");
        registry.addResourceHandler("/webjars/**").addResourceLocations(
                "classpath:/META-INF/resources/webjars/");
        super.addResourceHandlers(registry);
    }

}

你可能感兴趣的:(java)