SpringBoot集成Swagger接口管理工具

手写Api文档的几个痛点:

  • 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时。
  • 接口返回结果不明确
  • 不能直接在线测试接口,通常需要使用工具,比如postman
  • 接口文档太多,不好管理

Swagger也就是为了解决这个问题,当然也不能说Swagger就一定是完美的,当然也有缺点,最明显的就是代码移入性比较强,需要手动添加注解。其他的不多说,想要了解Swagger的,可以去Swagger官网,可以直接使用Swagger editor编写接口文档,当然我们这里讲解的是SpringBoot整合Swagger2,直接生成接口文档的方式。

Maven引入依赖项目

        <!--Swagger 框架-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.6.1</version>
        </dependency>

      <!--Swagger UI-->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.6.1</version>
        </dependency>

配置文件

在Spring中引入Bean,使用Java代码的方式,更加的方便注意:使用的注解信息

import org.springframework.context.annotation.Bean;
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;

/**
 * 本文件由周涛创建,位于com.tao.mybatis_plus.config包下
 * 创建时间2018/3/24 19:04
 * 邮箱:zhoutao@xiaodouwangluo.com
 * 作用:暂未填写
 *
 * @author tao
 */
@Configuration
@EnableSwagger2
public class Swagger2 {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                //设置扫描的包名
                .apis(RequestHandlerSelectors.basePackage("com.tao.mybatis_plus.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                //文档内容配置信息
                .title("SpringBoot整合Swagger")
                .description("这是一个简单的SpringBoot项目,基于Maven架构,SSM框架搭建")
                .termsOfServiceUrl("https://www.zhoutao123.com")
                .version("1.0")
                .build();
    }
}

配置Controller

下面的代码中主要用到了5个注解分别是:

  • RestController
  • RequestMapping
  • Autowired
  • GetMapping
  • ApiOperation

其中主要重点介绍ApiOperation


@RestController
@RequestMapping("/book")
public class IndexController {

    @Autowired
    private IBookService bookServer;

    /**
     * 查询图书列表
     * @param index 索引
     * @param page 数量
     * @return String
     */
    @ApiOperation(value = "查询图书列表",notes = "根据历史信息查询结果")
    @GetMapping("/list/{index}/{page}")
    public String index(@PathVariable("index") int index,@PathVariable("page") int page){

        Integer a = Integer.parseInt("123");

        Page<Book> bookPage = bookServer.selectPage(new Page<Book>(index, page));
        return JSONObject.toJSONString(bookPage).toString();
    }
}

测试

使用浏览器打开http://localhost:{port}/swagger-ui.html 即可看到Swagger界面,可以进行调试

原文地址:https://www.cnblogs.com/zhoutao825638/p/10382065.html