spring boot 中Swagger的使用

在开发过程中方便测试
1、依赖的包:
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.6.1</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-spring-web</artifactId>
<version>2.6.1</version>
</dependency>

2、配置类:
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket customDocket() {
        return new Docket(DocumentationType.SWAGGER_2).groupName("situation")
                .genericModelSubstitutes(DeferredResult.class).useDefaultResponseMessages(false).forCodeGeneration(true)
                .select().apis(RequestHandlerSelectors.basePackage("com.huawei.situation.controller"))// 选择哪些路径和API会生成document
                .paths(PathSelectors.any())// 对所有路径进行监控
                .build().apiInfo(apiInfo());
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Situation API")
                .description("TCB_Site_Situation Rest API with Swagger")
                .termsOfServiceUrl("/Situation/")
                .contact(new Contact("lwx480324", "", ""))
                .license("TCB_Site_Situation License")
                .licenseUrl("")
                .version("1.0")
                .build();
    }
}

3、在controller类上使用@Api定义controller的基本信息,在相关api接口上使用@ApiOperation、@ApiResponses来设置方法的一些基本信息、响应类型等信息
4、localhost:port/swagger-ui.html访问页面!

猜你喜欢

转载自www.cnblogs.com/xiaoxionganna/p/9935692.html