美文网首页Spring程序员
Spring Boot整合Swagger2构建RESTful A

Spring Boot整合Swagger2构建RESTful A

作者: 5e30faa7d323 | 来源:发表于2017-09-28 15:02 被阅读245次
开发中遇到的接口文档问题

在项目开发中一般分为前端UI开发和后端接口开发,前端通过调用后端的RESTful API接口获取数据然后展示。所以UI开发工程师需要知道后端的接口长什么样,比如URL是多少,输入、输出参数是什么,具体的数据格式是怎样的。往往后端开发工程师需要提供一份接口文档,这往往需要花很多时间来编写文档,而且一旦接口有变动,接口文档也要跟着一起维护,消耗了大量的时间。那么怎么解决这个问题呢?就是今天讲到的SWAGGER。

什么是Swagger2

借用Swagger2的定义:

Swagger2 是一款RESTFUL接口的文档在线自动生成+功能测试功能软件。Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。Swagger2让部署管理和使用功能强大的API从未如此简单。

Swagger2的官网:https://swagger.io/getting-started/

在Spring Boot中配置Swagger2
1.pom.xml中配置Swagger2相关jar

基于一个Spring Boot项目,见文章:Spring Boot 的HelloWorld详解
在pom.xml中加入如下配置:

<!--swagger相关的jar包 -->
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger-ui</artifactId>
            <version>2.4.0</version>
        </dependency>
        <dependency>
            <groupId>io.springfox</groupId>
            <artifactId>springfox-swagger2</artifactId>
            <version>2.4.0</version>
        </dependency>
2.配置Swagger2

建类SwaggerConfig作为Swagger的初始化类,代码如下:

package springboot;

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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;

/**
 * Created by Administrator on 2017/9/27.
 */
@Configuration
@EnableSwagger2
public class SwaggerConfig {

    @Bean
    public Docket createRestApi() {
        return new Docket(DocumentationType.SWAGGER_2)
                .apiInfo(apiInfo())
                .select()
                .apis(RequestHandlerSelectors.basePackage("springboot.controller"))
                .paths(PathSelectors.any())
                .build();
    }

    private ApiInfo apiInfo() {
        return new ApiInfoBuilder()
                .title("Spring Boot中使用Swagger2构建RESTful APIs")
                .description("Spring Boot REST API with Swagger")
                .termsOfServiceUrl("http://blog.didispace.com/")
                .contact(new Contact("Li Chengbang", "", "robin_lcb@sina.com"))
                .license("Apache License Version 2.0")
                .licenseUrl("http://www.apache.org/licenses/LICENSE-2.0.html")
                .version("1.0")
                .build();
    }
}

其中basePackage("springboot.controller")中的springboot.controller是需要进行注解来生成文档的包路径。

3.在controller中通过注解写api文档

代码如下:

@RestController
@Api(tags = "swagger测试", description = "主要展示swagger")
public class HelloWorldController {
    @ApiOperation(value="sayHello-test", notes="测试swagger")
    @RequestMapping(value="/",method = RequestMethod.GET)
    public String sayHello() {
        return "Hello,World!";
    }
}

主要的注解是@ApiOperation

4.运行App看效果

运行App后打开:http://localhost:8080/swagger-ui.html,见如下页面表示成功整合。

Paste_Image.png

具体的代码见github:https://github.com/liuahang/spring-boot-helloworld 的swagger分支。

相关文章

网友评论

    本文标题:Spring Boot整合Swagger2构建RESTful A

    本文链接:https://www.haomeiwen.com/subject/flomextx.html