spring boot 中Swagger的使用

本文详细介绍如何在SpringBoot项目中集成Swagger2,包括添加依赖、配置类编写、注解使用及页面访问方法,帮助开发者快速搭建RESTful API文档。
在开发过程中方便测试
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访问页面!

 

转载于:https://www.cnblogs.com/xiaoxionganna/p/9935692.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值