springboot继承swagger2 --- 在线文档的查阅和测试功能

1,090 阅读1分钟

1、添加Swagger2相关依赖

<!-- Swagger2依赖 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.7.0</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.7.0</version>
</dependency>

2、在xxxaplication.java启动类中添加相关注解

@EnableSwagger2 --- 开启Swagger2注解开发

3、添加Swagger2配置类

@Configuration
public class Swagger2 {
    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .genericModelSubstitutes(DeferredResult.class)
                .useDefaultResponseMessages(false)
                .forCodeGeneration(false)
                .pathMapping("/")
                .select()
                .build()
                .apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("XXXOA系统API文档")
                .version("1.0")
                .build();
    }
}

4、启动项目

打开 http://[IP]:[socket]/swagger-ui.html (ip,socket根据自己情况填写)

常用注解的使用方式

参考:Swagger2使用

参考文章

Swagger2配置