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

使用Spring Boot可以方便的集成Swagger2

1.新建Spring Boot项目

2.添加swagger依赖

<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>

---

3.创建swagger配置类

/**
* Created by LG on 2017/8/3.
*
* Spring Boot中使用Swagger2构建RESTful APIs说明文档
* 访问http://localhost:8071/swagger-ui.html 查看效果
*/
@Configuration
@EnableSwagger2
public class Swagger2 { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.luangeng.controller"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建RESTful APIs说明文档--title")
.description("Spring Boot中使用Swagger2构建RESTful APIs说明文档--description")
.contact("Spring Boot中使用Swagger2构建RESTful APIs说明文档--contact")
.version("1.0.1")
.build();
} }

---

4.添加一个UserController.java类

@RestController
@RequestMapping(value="/users")
public class UserController { private static Map<Long, User> users = Collections.synchronizedMap(new HashMap<Long, User>()); @ApiOperation(value="获取用户列表", notes="获取用户列表")
@RequestMapping(value="/", method= RequestMethod.GET)
public List<User> getUserList() {
List<User> r = new ArrayList<User>(users.values());
return r;
} @ApiOperation(value="创建用户", notes="根据User对象创建用户")
@ApiImplicitParam(name = "user", value = "实体user", required = true, dataType = "User")
@RequestMapping(value="/", method=RequestMethod.POST)
public String postUser(@ModelAttribute User user) {
users.put(user.getId(), user);
return "success";
} @RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
} @RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @ModelAttribute User user) {
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "success";
} @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
users.remove(id);
return "success";
} }

---

再增加一个InfoController.java类

@RestController
public class InfoController { @Autowired
MyConfig myconfig; @RequestMapping("/info")
public String msg() {
return "client1 service"+myconfig.toString();
} }

---

6.访问http://localhost:8071/swagger-ui.html 查看效果

+

end

Spring Boot 集成Swagger2生成RESTful API文档的更多相关文章

  1. Spring Boot 集成 Swagger 生成 RESTful API 文档

    原文链接: Spring Boot 集成 Swagger 生成 RESTful API 文档 简介 Swagger 官网是这么描述它的:The Best APIs are Built with Swa ...

  2. springboot集成swagger2构建RESTful API文档

    在开发过程中,有时候我们需要不停的测试接口,自测,或者交由测试测试接口,我们需要构建一个文档,都是单独写,太麻烦了,现在使用springboot集成swagger2来构建RESTful API文档,可 ...

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

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

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

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

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

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

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

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

  7. Golang使用swaggo自动生成Restful API文档

    #关于Swaggo 相信很多程序猿和我一样不喜欢写API文档.写代码多舒服,写文档不仅要花费大量的时间,有时候还不能做到面面具全.但API文档是必不可少的,相信其重要性就不用我说了,一份含糊的文档甚至 ...

  8. 如何生成RestFul Api文档

    Web API文档工具列表Swagger ——Swagger框架可以通过代码生成漂亮的在线API,甚至可以提供运行示例.支持Scala.Java.Javascript.Ruby.PHP甚至 Actio ...

  9. Api2Doc生成 Restful API 文档

    1,引入maven <dependency> <groupId>com.github.terran4j</groupId> <artifactId>te ...

随机推荐

  1. INSPIRED启示录 读书笔记 - 第5章 产品管理与软件开发

    保持融洽的合作关系 形成合作关系的关键是双方承认彼此平等——任何一方不从属于另一方,产品经理负责定义正确的产品,开发团队负责正确地开发产品,双方相互依赖 产品经理要求开发团队完成任务,必须先取得他们的 ...

  2. java基础之final/static/static final

    一.final 1.final修饰变量(常量) final修饰的成员变量表示常量,一旦给定初值既无法改变 2.final方法 final修饰方法,表示该方法不能被子类重写 好处:比非final方法要快 ...

  3. R语言笔记002——sample()函数

    sample()函数 sample(x,size,replace=FALSE) x表示一个或多个向量,size表示从x中随机取的样本个数,replace=FALSE表示不放回抽样,即不会选取到相同的值 ...

  4. jQuery颜色面板插件

    /** * jQuery颜色面板插件 * * 使用方法:input框的id默认为inputObj,button框的id默认为btnObj,也可以自定义aaa,bbb * 1.初始化颜色面板:$.col ...

  5. Proxy动态代理

    Proxy动态代理 package com.test.dynamicproxy; public interface Subject { public void request(); } package ...

  6. HDU 5992 kd-tree

    还记得青岛的时候和蕾姐讨论了近三个小时也不知道这是什么东西 后来发现是kdtree 于是拖到寒假才补这个算法 写完几道模板题发现多维的kdtree查找最近也是很简单的拓展 于是很快1A了这道题 它真的 ...

  7. @RequestMapping

    可以设定访问的目录,与访问的方式 对象可以是类,也可以是方法 @RequestMapping(value = "/say",method = RequestMethod.GET) ...

  8. Python 正则表达式匹配小数

    不废话,直接上表达式 (\d+(\.\d+)?) 如: import re find_float = lambda x: re.search("\d+(\.\d+)?",x) .g ...

  9. nova shelve

    当一个虚机不需要使用的时候,可以将其 shelve 起来.该操作会创建该虚机的一个快照并传到 Glance 中,然后在 Hypervisor 上将该虚机删除,从而释放其资源. 其主要过程为: dest ...

  10. 定义类+类实例化+属性+构造函数+匿名类型var+堆与栈+GC回收机制+值类型与引用类型

    为了让编程更加清晰,把程序中的功能进行模块化划分,每个模块提供特定的功能,而且每个模块都是孤立的,这种模块化编程提供了非常大的多样性,大大增加了重用代码的机会. 面向对象编程也叫做OOP编程 简单来说 ...