天天看点

SwaggerUI+SpringMVC——构建RestFul API的可视化界面

今天给大家介绍一款工具,这个工具目前可预见的好处是:自动维护最新的接口文档。

我们都知道,接口文档是非常重要的,但是随着代码的不断更新,文档却很难持续跟着更新,今天要介绍的工具,完美的解决了这个问题。而且,对于要使用我们接口的人来说,不需要在给他提供文档,告诉他地址,一目了然。

最近项目中一直有跟接口打交道,恰好又接触到了一个新的接口工具,拿出来跟大家分享一下。

关于REST接口,我在上篇文章中已经有介绍,这里来说一下如何配合SwaggerUI搭建RestFul API 的可视化界面。最终要达到的效果是这样的:

它可以支持Rest的所有提交方式,如POST,GET,PUT,DELETE等。

这里可以看到我们的方法注释,需要的参数,参数的类型和注释,返回值的类型注释等信息,最重要的,我们这里可以直接对REST接口测试。

接下来,我们一起开始逐步实现如图的效果

第一步:首先,引入依赖的jar包

<span style="white-space:pre">    </span><!-- swagger -->

    <dependency>

        <groupId>com.mangofactory</groupId>

        <artifactId>swagger-springmvc</artifactId>

        <version>0.9.5</version>

    </dependency>

    <dependency>

            <groupId>com.fasterxml.jackson.core</groupId>

            <artifactId>jackson-annotations</artifactId>

            <version>2.4.4</version>

        </dependency>

        <dependency>

            <groupId>com.fasterxml.jackson.core</groupId>

            <artifactId>jackson-databind</artifactId>

            <version>2.4.4</version>

        </dependency>

        <dependency>

            <groupId>com.fasterxml.jackson.core</groupId>

            <artifactId>jackson-core</artifactId>

            <version>2.4.4</version>

        </dependency>

第二步,创建swagger配置文件类,基本不用改,只需要修改要匹配的方法路径即可。

package com.gochina.mis.util;

import org.springframework.beans.factory.annotation.Autowired;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.ComponentScan;

import org.springframework.context.annotation.Configuration;

import com.mangofactory.swagger.configuration.SpringSwaggerConfig;

import com.mangofactory.swagger.models.dto.ApiInfo;

import com.mangofactory.swagger.plugin.EnableSwagger;

import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin;

@Configuration

@EnableSwagger

public class SwaggerConfig {

    private SpringSwaggerConfig springSwaggerConfig;

    @Autowired

    public void setSpringSwaggerConfig(SpringSwaggerConfig springSwaggerConfig) {

        this.springSwaggerConfig = springSwaggerConfig;

    }

    @Bean

    public SwaggerSpringMvcPlugin customImplementation()

    {

        return new SwaggerSpringMvcPlugin(this.springSwaggerConfig).apiInfo(apiInfo()).includePatterns("/album

public class BaseVo {

    @ApiModelProperty(value = "状态") 

    private Boolean success;

    @ApiModelProperty(value = "消息")

    private String msg;

    @ApiModelProperty(value = "服务器当前时间戳,sample: 1434553831")

    private Long currentTime = new Timestamp(System.currentTimeMillis()).getTime();

    public Boolean getSuccess() {

        return success;

    }

    public void setSuccess(Boolean success) {

        this.success = success;

    }

    public String getMsg() {

        return msg;

    }

    public void setMsg(String message) {

        this.msg = message;

    }

    public Long getCurrentTime() {

        return currentTime;

    }

    public void setCurrentTime(Long currentTime) {

        this.currentTime = currentTime;

    }

}

运行访问:http://localhost:8080/api-doc/index.html ,当然,我们也可以对这个页面加权限验证

大功告成!对于开发人员来说,每个接口只需要添加一些注解,SwaggerUI会自动生成如我们文章开始时展现的页面,方便调用和测试。

--------------------- 

转载于:https://blog.csdn.net/libaoqiang613/article/details/47398715