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. 七 、linux正则表达式

    为处理大量的字符串而定义的一套规则和方法 1)linux正则表达式以行为单位处理 2)alians grep = “grep –color=auto”,让匹配的内容显示颜色 3)注意字符集,expor ...

  2. nodejs往文件写入内容代码

    const fs = require("fs"); // fs.wirteFile有三个参数 // 1,第一个参数是要写入的文件路径 // 2,第二个参数是要写入得内容 // 3, ...

  3. RDLC 微软报表 导出Excel时产生多个工作表 (worksheet)

    . I have added two obejcts data source to Report Viewer. 2. in RDLC i have created two tables and in ...

  4. IOS 发布被拒 3.2 f

    Dear Developer, We have determined that your Apple Developer Program membership, or another membersh ...

  5. Ceph简介

    最近偶尔接触到云计算,开始对云计算感兴趣,希望能够早日加入这个计算机领域的第三次革命中去. 估计这次革命要持续二十年,也就是这辈子一直干云计算都没问题. 先了解一下Ceph吧.本博文主要是根据文献[1 ...

  6. python中的import一个注意事项

    import math def foo(): import math x = math.pi # 如果math在下面import会出错,因为import是个写的过程(添加到sys.modules中), ...

  7. 在Windows下使用adb logcat grep

    在Windows下使用adb logcat  grep 会提示 因为grep 为Linux命令,所以不能使用.怎么办呢? 这时候可以用到babun 下载地址:http://babun.github.i ...

  8. python中常见的日期换算

    time模块提供各种操作时间的函数  说明:一般有两种表示时间的方式:       第一种是时间戳的方式(相对于1970.1.1 00:00:00以秒计算的偏移量),时间戳是惟一的       第二种 ...

  9. EF-简化排序

    respository.GetPaged<S_Users>(out count, m => m.LoginName.Contains("a"),"Log ...

  10. php实现微信扫码自动登陆与注册功能

    本文实例讲述了php实现微信扫码自动登陆与注册功能.分享给大家供大家参考,具体如下: 微信开发已经是现在程序员必须要掌握的一项基本的技术了,其实做过微信开发的都知道微信接口非常的强大做起来也非常的简单 ...