Swagger 是一个规范和完整的框架,用于生成、描述、调用和可视化 RESTful 风格的 Web 服务。总体目标是使客户端和文件系统作为服务器以同样的速度来更新。文件的方法,参数和模型紧密集成到服务器端的代码,允许API来始终保持同步。

开始

1、pom.xml 添加依赖:

<!-- swagger RESTful API 文档 -->
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>

2、创建 User 实体

package com.sam.demo.domain;

/**
* @author sam
* @since 2017/7/17
*/
public class User { private Long id;
private String name;
private int age; //getter & setter
}

3、在 Application.java 同级创建 Swagger2.java

package com.sam.demo;

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; /**
* @author sam
* @since 2017/7/17
*/
@Configuration
@EnableSwagger2
public class Swagger2 { @Bean
public Docket createRestApi() { ApiInfo apiInfo = new ApiInfoBuilder()
.title("Sam 项目接口文档")
.description("Magical Sam 项目的接口文档,符合RESTful API。")
.version("1.0")
.build(); return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo)
.select()
.apis(RequestHandlerSelectors.basePackage("com.sam.demo.controller")) //以扫描包的方式
.paths(PathSelectors.any())
.build();
} }

其中 .apis(RequestHandlerSelectors.basePackage("com.sam.demo.controller")) 指定了以扫描包的方式进行,会把com.sam.demo.controller包下的controller都扫描到。

4、创建 UserController.java

package com.sam.demo.controller;

import com.sam.demo.domain.User;
import io.swagger.annotations.*;
import org.springframework.web.bind.annotation.*; import java.util.ArrayList;
import java.util.List; /**
* @author sam
* @since 2017/7/14
*/
@Api(value = "用户模块")
@RestController
@RequestMapping("/user")
public class UserController { /**
* 获取单个用户
*
* @param id
* @return
*/
@ApiOperation(value = "获取单个用户", notes = "传入id获取单个用户")
// @ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Long") //注意:paramType需要指定为path,不然不能正常获取
@RequestMapping(value = "/{id}", method = RequestMethod.GET)
public String user(@ApiParam(value = "用户Id", required = true) @PathVariable Long id) {
return "user id :" + id;
} /**
* 获取用户列表
*
* @return
*/
@ApiOperation(value = "获取用户列表", notes = "获取用户列表")
@RequestMapping(value = "", method = RequestMethod.GET)
public List list() {
List list = new ArrayList();
list.add("Sam1");
list.add("Sam2");
list.add("Sam3");
return list;
} /**
* 新建用户
*
* @param user
* @return
*/
@ApiOperation(value = "新建用户", notes = "新建一个用户")
// @ApiImplicitParams({
//注意:paramType需要指定为body
// @ApiImplicitParam(name = "user", value = "用户数据", required = true, paramType = "body", dataType = "User")
// })
@RequestMapping(value = "", method = RequestMethod.POST)
public String create(@ApiParam(value = "用户数据", required = true) @RequestBody User user) {
System.out.println("user : " + user.getName() + " " + user.getAge());
return "success 新建user : " + user.getName() + " " + user.getAge();
} /**
* 删除用户
*
* @return
*/
@ApiOperation(value = "删除用户", notes = "通过用户id删除用户")
@ApiImplicitParam(name = "id", value = "用户id", required = true, paramType = "path", dataType = "Long")
@RequestMapping(value = "/{id}", method = RequestMethod.DELETE)
public String delete(@PathVariable Long id) {
System.out.println("删除用户:" + id);
return "success 删除user" + id;
} /**
* 更新用户
*
* @return
*/
@ApiOperation(value = "更新用户", notes = "更新已存在用户")
@ApiImplicitParam(name = "user", value = "用户数据", required = true, paramType = "body", dataType = "User")
@RequestMapping(value = "", method = RequestMethod.PUT)
public String update(@RequestBody User user) {
System.out.println("更新用户:" + user.getId() + " " + user.getName() + " " + user.getAge());
return "success 更新user : " + user.getId() + " " + user.getName() + " " + user.getAge();
} }

启动应用,浏览器访问:http://localhost:8080/swagger-ui.html

正常展示 api 接口文档界面,如下:

注意1:@ApiImplicitParam 和 @ApiParam 方式均能指定参数规则。

注意2:使用@ApiImplicitParam的时候,需要指定paramType。

附:Swagger2相关注解介绍

@Api:用在类上,说明该类的作用
@ApiOperation:用在方法上,说明方法的作用
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiImplicitParam:用在 @ApiImplicitParams 注解中,指定一个请求参数的各个方面
paramType:参数放在哪个地方
· header --> 请求参数的获取:@RequestHeader
· query -->请求参数的获取:@RequestParam
· path(用于restful接口)--> 请求参数的获取:@PathVariable
· body(不常用)
· form(不常用)
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的意思
defaultValue:参数的默认值
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性

版权声明:本文为博主原创文章,转载请注明出处。

Spring Boot 系列(七)Swagger2-生成RESTful接口文档的更多相关文章

  1. Spring Boot中使用Swagger2生成RESTful API文档(转)

    效果如下图所示: 添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!-- https://mvnrepository.com/artifact/io.springfox ...

  2. Spring Boot中使用Swagger2构建RESTful API文档

    在开发rest api的时候,为了减少与其他团队平时开发期间的频繁沟通成本,传统做法我们会创建一份RESTful API文档来记录所有接口细节,然而这样的做法有以下几个问题: 1.由于接口众多,并且细 ...

  3. Swagger+Spring mvc生成Restful接口文档

    简介 Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总体目标是使客户端和文件系统作为服务器以同样的速度来更新.文件的方法,参数和模型紧密集 ...

  4. Spring Boot 集成Swagger2生成RESTful API文档

    Swagger2可以在写代码的同时生成对应的RESTful API文档,方便开发人员参考,另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API. 使用Spring Boot可 ...

  5. springboot项目利用Swagger2生成在线接口文档

    Swagger简介. Swagger2是一款restful接口文档在线生成和在线调试工具.很多项目团队利用Swagger自动生成接口文档,保证接口文档和代码同步更新.在线调试.简单地说,你可以利用这个 ...

  6. Spring Boot中使用Swagger2自动构建API文档

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  7. Spring Boot2配置Swagger2生成API接口文档

    一.Swagger2介绍 前后端分离开发模式中,api文档是最好的沟通方式. Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务. 及时性 (接 ...

  8. Spring MVC学习总结(9)——Spring MVC整合swagger自动生成api接口文档

    Swagger 号称:世界最流行的API框架,官网:http://swagger.io/,Swagger 是一个规范和完整的框架,用于生成.描述.调用和可视化 RESTful 风格的 Web 服务.总 ...

  9. Spring Boot 入门系列(二十二)使用Swagger2构建 RESTful API文档

    前面介绍了如何Spring Boot 快速打造Restful API 接口,也介绍了如何优雅的实现 Api 版本控制,不清楚的可以看我之前的文章:https://www.cnblogs.com/zha ...

  10. Spring Boot学习笔记 - 整合Swagger2自动生成RESTful API文档

    1.添加Swagger2依赖 在pom.xml中加入Swagger2的依赖 <!--swagger2--> <dependency> <groupId>io.spr ...

随机推荐

  1. (Swiftmailer)高效的PHP邮件发送库

    Swiftmailer是一个类似PHPMailer邮件发送组件,它也支持HTML格式.附件发送,但它发送效率相当高,成功率也非常高,很多PHP框架都集成了Swiftmailer. Swiftmaile ...

  2. Spring资源加载器抽象和缺省实现 -- ResourceLoader + DefaultResourceLoader(摘)

    概述 对于每一个底层资源,比如文件系统中的一个文件,classpath上的一个文件,或者一个以URL形式表示的网络资源,Spring 统一使用 Resource 接口进行了建模抽象,相应地,对于这些资 ...

  3. 【转】javaUDP套接字通信

    Java UDP网络编程 - 最简单示例   转自 http://blog.csdn.net/wintys/article/details/3525643 /** *UDPServer *@autho ...

  4. 直播流RTMP 知识

    分享直播相关知识点: http://blog.csdn.net/kingroc/article/details/50839994 #!/bin/bash# Order Finish Startup# ...

  5. Unity3D编辑器扩展(三)——使用GUI绘制窗口

    前两篇分别讲解了创建菜单https://www.cnblogs.com/xiaoyulong/p/10115053.html和创建窗口https://www.cnblogs.com/xiaoyulon ...

  6. require()  module.export    Object.keys()

    import API from"../../api/api.js";   var data = require('../../utils/data.js').songs;   // ...

  7. leetcode第一天-merge two binary trees

    有段时间没有写代码了,脑子都生锈了,今后争取笔耕不辍(立flag,以后打脸) 随机一道Leecode题, Merge Two Binary Trees,题目基本描述如下: Given two bina ...

  8. Javascript高级编程学习笔记(27)—— BOM(1)window对象1

    ECMAScript是JS的核心 但是对于在浏览器中运行的JS,BOM显然才是真正的核心 我们知道JS是由三个部分组成的 BOM.DOM.ECMAScript 之前的文章我们主要介绍的是ECMAScr ...

  9. 每天学点SpringCloud(十三):SpringCloud-Stream整合RabbitMQ

    我们知道,当微服务越来越来多的时候,仅仅是feign的http调用方式已经满足不了我们的使用场景了.这个时候系统就需要接入消息中间件了.相比较于传统的Spring项目.SpringBoot项目使用消息 ...

  10. Ubuntu18.04或者Deepin15.8 部署Django项目

    一.首先先安装nginx静态服务 1.安装gcc g++的依赖库sudo apt-get install build-essential && sudo apt-get install ...