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. Docker 监控平台Prometheus

    Prometheus 是一个强大的监控平台,提供了监控数据搜集.存储.处理.可视化和告警一套完整的解决方案. 官方网站:https://prometheus.io

  2. 【bzoj1036】树的统计[ZJOI2008]树链剖分+线段树

    题目传送门:1036: [ZJOI2008]树的统计Count 这道题是我第一次打树剖的板子,虽然代码有点长,但是“打起来很爽”,而且整道题只花了不到1.5h+,还是一遍过样例!一次提交AC!(难道前 ...

  3. JAVA 写中文字符串到指定文件 中文乱码 问题解决

    之前试过下面代码里面的注释掉的 方法,都不行,后来想到了不如指定编码格式试试,果真可以了. String as= “中文字符”; //byte[] b = as.getBytes(); try{ Fi ...

  4. 关于hadoop setCombinerClass 与 setReducerClass同时使用存在的问题。

    最近在学习hadoop mapreduce编程的过程中遇到一个莫名奇妙的问题.最后通过调试时发现同时使用setCombinerClass(Reducer.class)  与  setReducerCl ...

  5. QT QFtp使用实例 从FTP下载一个文件

    1. ftp://ftp.denx.de/pub/u-boot/lowboot-1.0.0.patch.gz  下载文件 FtpGet.h #ifndef FTPGET_H #define FTPGE ...

  6. Java 通过JDBC连接Mysql数据库的方法和实例【图文说明】

    JDBC(Java Data Base Connectivity,java数据库连接)是一种用于执行SQL语句的Java API,可以为多种关系数据库提供统一访问,它由一组用Java语言编写的类和接口 ...

  7. 【转】ORACLE的数据类型

    原文;http://linjian004.bokee.com/3916067.html 常用的数据库字段类型如下: 字段类型 中文说明 限制条件 其它说明 CHAR 固定长度字符串 最大长度2000 ...

  8. write.table函数语法:

    write.table (x,  file ="",  sep ="",  row.names =TRUE,  col.names =TRUE,  quote ...

  9. web测试中的测试点和测试方法总结

    测试是一种思维,包括情感思维和智力思维,情感思维主要体现在一句俗语:思想决定行动上(要怀疑一切),智力思维主要体现在测试用例的设计上.具有了这样的思想,就会找出更多的bug.   一.输入框 1.字符 ...

  10. web自动化:元素定位(二)

    一. 实例 如何定位到下图第二个"抢投标",有一种方法是利用xpath定位 //a[@href="/loan/loan_detail/Id/7190.html" ...