Springboot集成Swagger文档

Springboot集成Swagger文档

使用Swagger集成文档的优势:
  1.支持在见面测试API接口功能 。
  2.能够及时更新Api文档 。
  3.整合简单 。

1.1  添加依赖

  在pom文件里添加Mave依赖。pom.xml

        <!-- Swagger -->
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger2</artifactId>
			<version>2.9.2</version>
		</dependency>
		<dependency>
			<groupId>io.springfox</groupId>
			<artifactId>springfox-swagger-ui</artifactId>
			<version>2.9.2</version>
		</dependency>

1.2  配置类

  在src下新建config包,并在包下添加Swapper配置类:SwapperConfig.java

import org.springframework.context.annotation.Configuration;
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;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

@Configuration
@EnableSwagger2
public class SwaggerConfig {
    public Docket createRestApi(){
        return new Docket(DocumentationType.SWAGGER_2)
                   .apiInfo(apiInfo())
                   .select()
                   .apis(RequestHandlerSelectors.any())
                   .paths(PathSelectors.any())
                   .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder().build();
    }
}

1.3  页面测试

  在编译器上启动项目,在浏览器上输入访问地址:http://localhost:8006/swagger-ui.html#/,会看到Swaaper的接口文档界面,如下图所示:
在这里插入图片描述
  单击Hello接口,然后点击try it out ,最后点击execute,发现接口成功的返会回:“Hello Word!” 信息,如下图所示:
在这里插入图片描述
今后的开发过程中,就可以通过 Swagger 来测试接口了,是不是很方便呀。嘻嘻嘻、!

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值