Swagger基本介绍

Swagger是创建交互式REST API文档的规范和框架,它能自动同步REST服务的任何变化,同时为生成API客户端代码提供了一套工具和SDK生成器。Swagger规范由两种文件类型组成:资源文件(包含一系列文件)和一套API声明文件(描述了REST API和可用的操作)。资源文件是API声明文件的根,它描述了一般信息,比如API版本、title、描述、license,同时它也包含了所有可用的API资源。API声明文件描述了带有API操作和请求/响应展现的资源,basePath域提供了API的根URI,resourcePath指定了相对于basePath的资源路径,apis域包含了描述API操作的接口对象,models域包含了和资源相关的模型对象。

Swagger使用JSON作为描述语言。

集成Swagger

在POM文件中加入如下依赖:

<dependency>
<groupId>com.mangofactory</groupId>
<artifactId>swagger-springmvc</artifactId>
<version>1.0.2</version>
</dependency>

然后通过@EnableSwagger注解激活swagger-springmvc。

Swagger UI

Swagger UI是Swagger的一个子项目,能够利用资源文件和API描述文件为API自动生成友好的、可交互的接口。

在应用中集成Swagger UI的方法是:首先从https://github.com/swagger-api/swagger-ui下载Swagger UI的稳定版本,然后dist文件夹下的内容移动到应用的类路径下(一般放到resoures目录下)。更改index.html文件中的如下内容

$(function () {
window.swaggerUi = new SwaggerUi({
url: "http://localhost:8080/api-docs",
dom_id: "swagger-ui-container",
// code removed for brevity
}

最后通过http://localhost:8080/swagger-ui/index.html启动Swagger UI。

定制Swagger

可通过在应用中建立一个配置类实现对Swagger的定制。

import javax.inject.Inject;

import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration; import com.mangofactory.swagger.configuration.SpringSwaggerConfig;
import com.mangofactory.swagger.models.dto.ApiInfo;
import com.mangofactory.swagger.models.dto.builder.ApiInfoBuilder;
import com.mangofactory.swagger.plugin.EnableSwagger;
import com.mangofactory.swagger.plugin.SwaggerSpringMvcPlugin; @Configuration
@EnableSwagger
public class SwaggerConfig { @Inject
private SpringSwaggerConfig springSwaggerConfig; private ApiInfo getApiInfo() { ApiInfo apiInfo = new ApiInfoBuilder()
.title("QuickPoll REST API")
.description("QuickPoll Api for creating and managing polls")
.termsOfServiceUrl("http://example.com/terms-of-service")
.contact("info@example.com")
.license("MIT License")
.licenseUrl("http://opensource.org/licenses/MIT")
.build(); return apiInfo;
} @Bean
public SwaggerSpringMvcPlugin v1APIConfiguration() {
SwaggerSpringMvcPlugin swaggerSpringMvcPlugin = new SwaggerSpringMvcPlugin(this.springSwaggerConfig);
swaggerSpringMvcPlugin
.apiInfo(getApiInfo()).apiVersion("1.0")
.includePatterns("/v1/*.*").swaggerGroup("v1");
swaggerSpringMvcPlugin.useDefaultResponseMessages(false);
return swaggerSpringMvcPlugin;
}
}

配置控制器

@API注解标注一个类为Swagger资源,Swagger会扫描标注了 @API的类,读取metadata生成资源文件和API描述文件。

@RestController
@Api(value = "polls", description = "Poll API")
public class PollController {
// Implementation removed for brevity
}

@ApiOperation注解用于标注API,可以自定义操作信息,比如名字、描述、响应。

@RequestMapping(value="/polls", method=RequestMethod.POST)
@ApiOperation(value = "API概要描述", notes="详细描述信息", response = Void.class)
public ResponseEntity<Void> createPoll(@Valid @RequestBody Poll poll) {
.......
}

@ApiResponse注解用于配置状态码和相关响应body。

RequestMapping(value="/polls", method=RequestMethod.POST)
@ApiOperation(value = "API概要描述", notes="详细描述信息", response = Void.class)
@ApiResponses(value = {@ApiResponse(code=201, message="Poll Created Successfully", response=Void.class),
@ApiResponse(code=500, message="Error creating Poll", response=ErrorDetail.class) } )
public ResponseEntity<Void> createPoll(@Valid @RequestBody Poll poll) {
// Content removed for brevity
}

配置UI

更改swagger-ui-wrap内容,将相关信息更改为应用相关的信息,如下所示:

<a id="logo" href="http://localhost:8080">QuickPoll</a>
<form id='api_selector'>
<div class='input'><input placeholder="http://example.com/api" id="input_baseUrl" name="baseUrl" type="text"/></div>
<div class='input'><input placeholder="api_key" id="input_apiKey" name="apiKey" type="text"/></div>
<div class='input'><a id="explore" href="#">Explore</a></div>
</form>

Spring REST实践之Documenting REST Services的更多相关文章

  1. 使用 Spring 3 来创建 RESTful Web Services(转)

    使用 Spring 3 来创建 RESTful Web Services 在 Java™ 中,您可以使用以下几种方法来创建 RESTful Web Service:使用 JSR 311(311)及其参 ...

  2. 基于Spring设计并实现RESTful Web Services(转)

    基于Spring设计并实现RESTful Web Services 在本教程中,你将会使用Spring来创建一个具有生产力的RESTful网络服务. 为什么用RESTful网络服务? 从和Amazon ...

  3. Spring+MyBatis实践—MyBatis数据库访问

    关于spring整合mybatis的工程配置,已经在Spring+MyBatis实践—工程配置中全部详细列出.在此,记录一下几种通过MyBatis访问数据库的方式. 通过sqlSessionTempl ...

  4. Spring MVC 实践 - Component

    Spring MVC 实践 标签 : Java与Web Converter Spring MVC的数据绑定并非没有任何限制, 有案例表明: Spring在如何正确绑定数据方面是杂乱无章的. 比如: S ...

  5. Spring MVC 实践 - Base

    Spring MVC 实践 标签 : Java与Web Spring Web MVC Spring-Web-MVC是一种基于请求驱动的轻量级Web-MVC设计模式框架, Spring MVC使用MVC ...

  6. Spring Boot实践——Spring AOP实现之动态代理

    Spring AOP 介绍 AOP的介绍可以查看 Spring Boot实践——AOP实现 与AspectJ的静态代理不同,Spring AOP使用的动态代理,所谓的动态代理就是说AOP框架不会去修改 ...

  7. Spring Boot实践——AOP实现

    借鉴:http://www.cnblogs.com/xrq730/p/4919025.html     https://blog.csdn.net/zhaokejin521/article/detai ...

  8. CRUD using Spring MVC 4.0 RESTful Web Services and AngularJS

    国内私募机构九鼎控股打造APP,来就送 20元现金领取地址:http://jdb.jiudingcapital.com/phone.html内部邀请码:C8E245J (不写邀请码,没有现金送)国内私 ...

  9. Spring Boot 实践 :Spring Boot + MyBatis

    Spring Boot 实践系列,Spring Boot + MyBatis . 目的 将 MyBatis 与 Spring Boot 应用程序一起使用来访问数据库. 本次使用的Library spr ...

随机推荐

  1. 用 Xcode 开发 Cydia Substrate 插件(二)

    上次介绍了一个如何用 Xcode 来构建 Substrate 插件,但是开发的具体过程还没有涉及,而这往往又正是初学者最难下手的地方,所以有了本文的后续. 不过在开始之前你要先做好思想准备,相比较开发 ...

  2. Vagrant使用笔记

    vagrant box add [options] <name, url, or path> - 添加box至vagrant的管理列表 vagrant init 初始化虚拟机至当前文件夹并 ...

  3. js处理日期的一些整理(js获取给定日期前一天的日期)

    var date = new Date(); alert(date);//获取当前时间 alert(date.getFullYear());//获取当前年分 alert(date.getMonth() ...

  4. vsftpd2.3.2安装、配置详解

    一.vsftpd 简介     Vsftpd是一个基于GPL发布的类UNIX系统的ftp服务器软件.其全称是Very Secure FTP Deamon,在安全性.速度和稳定性都有着不俗的表现.在安全 ...

  5. android中的style部分属性值介绍

    转自:http://blog.sina.com.cn/s/blog_70c759fd01013phv.html Android平台定义的主题样式: android:theme="@andro ...

  6. Mysql数据库优化总结2

    说明:本文的环境为CENTOS 5.5 64 Bit /Mysql 5.1.50 简介:使用Mysql有一段时间了,期间做了不少关于Mysql优化.设计.维护的工作,这两天有时间做一下简单的总结,方便 ...

  7. C#隐式运行CMD命令(隐藏命令窗口)

    原文 C#隐式运行CMD命令(隐藏命令窗口) 本文实现了C#隐式运行CMD命令的功能.下图是实例程序的主画面.在命令文本框输入DOS命令,点击"Run"按钮,在下面的文本框中输出运 ...

  8. 1、android源代码下载与跟踪

     学习Android源代码的目的 理解Android API查找API(Activity.Content Provider等) 高级应用开发(ROM定制)  在不同平台下载Android源代码 W ...

  9. 通过DDOS攻击流程图来浅谈如何预防Ddos攻击与防御

    DDOS攻击流程图 站长之家配图(来源:ppkj.net) 一 背景 在前几天,我们运营的某网站遭受了一次ddos攻击,我们的网站是一个公益性质的网站,为各个厂商和白帽子之间搭建一个平台以传递安全问题 ...

  10. Provider 错误 '80004005' 未指定的错误 的最终解决方法

    今天在配置公司的香港WEB服务器Server2003系统,建好应用程序池后,发现远行程序经常出现下面的错误,刷新几下又可以,但过不了多久又是出现下面的错误!! 在网上查找相关问题得知,这是2003SP ...