本文将介绍RESTful API的重磅好伙伴Swagger2,它可以轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。

它既可以减少我们创建文档的工作量,同时说明内容又整合入实现代码中,让维护文档和修改代码整合为一体,可以让我们在修改代码逻辑的同时方便的修改文档说明。

另外Swagger2也提供了强大的页面测试功能来调试每个RESTful API。

添加Swagger2依赖

在pom.xml中添加Swagger2的依赖

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

创建Swagger2的配置类

在com.qhong包下创建Swagger2类

@Configuration
@EnableSwagger2
public class Swagger2 { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.qhong.web"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("Spring Boot中使用Swagger2构建Restful Api")
.description("更多Spring Boot相关文章请关注:http://www.baidu.com/")
.termsOfServiceUrl("http://www.baidu.com/")
.contact("qhong")
.version("1.0")
.build();
}
}

添加Controller接口

创建com.qhong.domain包,在里面创建User类

public class User {
private Long id;
private String name;
private Integer age; public Long getId() {
return id;
} public void setId(Long id) {
this.id = id;
} public String getName() {
return name;
} public void setName(String name) {
this.name = name;
} public Integer getAge() {
return age;
} public void setAge(Integer age) {
this.age = age;
}
}

在com.qhong.web下创建UserController类

@RestController
@RequestMapping(value="/users") public class UserController { 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(@RequestBody User user) {
users.put(user.getId(), user);
return "success";
} @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.GET)
public User getUser(@PathVariable Long id) {
return users.get(id);
} @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
@ApiImplicitParams({
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
@ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
})
@RequestMapping(value="/{id}", method=RequestMethod.PUT)
public String putUser(@PathVariable Long id, @RequestBody User user) {
User u = users.get(id);
u.setName(user.getName());
u.setAge(user.getAge());
users.put(id, u);
return "success";
} @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
@ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
@RequestMapping(value="/{id}", method=RequestMethod.DELETE)
public String deleteUser(@PathVariable Long id) {
users.remove(id);
return "success";
}
}

@ApiOperation

@ApiImplicitParam

@ApiImplicitParams

用于描述创建的api

编译运行:

在HelloController中

@RestController
public class HelloController { //@ApiIgnore
@RequestMapping(value="/hello",method= RequestMethod.GET)
public String Index(){
return "hello world!";
}
}

如果添加@ApiIgnore,该Controller就会被忽略。

参考:http://blog.didispace.com/springbootswagger2/

来源:http://www.cnblogs.com/hongdada/p/5892927.html

RESTful API的重磅好伙伴Swagger2的更多相关文章

  1. Restful Api CRUD 标准示例 (Swagger2+validator)

    为什么要写这篇贴? 要写一个最简单的CRUD 符合 Restful Api    规范的  一个Controller, 想百度搜索一下 直接复制拷贝 简单修改一下 方法内代码. 然而, 搜索结果让我无 ...

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

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

  3. 使用Swagger2构建强大的RESTful API文档(1)(二十二)

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

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

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

  5. Spring Boot 中使用 Swagger2 构建强大的 RESTful API 文档

    项目现状:由于前后端分离,没有很好的前后端合作工具. 由于接口众多,并且细节复杂(需要考虑不同的HTTP请求类型.HTTP头部信息.HTTP请求内容等),高质量地创建这份文档本身就是件非常吃力的事,下 ...

  6. Spring Boot教程(二十二)使用Swagger2构建强大的RESTful API文档(1)

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

  7. 使⽤Swagger2构建强⼤的RESTful API⽂档

    使⽤Swagger2构建强⼤的RESTful API⽂档 导语: 由于Spring Boot能够快速开发.便捷部署等特性,相信有很⼤⼀部分Spring Boot的⽤户会⽤来构建RESTful API. ...

  8. Spring MVC中使用 Swagger2 构建Restful API

    1.Spring MVC配置文件中的配置 [java] view plain copy <!-- 设置使用注解的类所在的jar包,只加载controller类 --> <contex ...

  9. Spring MVC 中使用 Swagger2 构建动态 RESTful API

    当多终端(WEB/移动端)需要公用业务逻辑时,一般会构建 RESTful 风格的服务提供给多终端使用. 为了减少与对应终端开发团队频繁沟通成本,刚开始我们会创建一份 RESTful API 文档来记录 ...

随机推荐

  1. [Android] Web Console: Uncaught TypeError: Object [object Object] has no method 'xxx'

    我们开发的产品,有一部分功能,需要在WebView中打开web页面,然后在web页面中通过js方法回调部分native的功能. 对于web回调native的开发方式,如果不了解的话,可以参考我以前的一 ...

  2. java写hadoop全局排序

    前言: 一直不会用java,都是streaming的方式用C或者python写mapper或者reducer的可执行程序.但是有些情况,如全排序等等用streaming的方式往往不好处理,于是乎用原生 ...

  3. scrapy数据存入mongodb

    存入mongodb的pipelines文件是这样子写的 from openpyxl import Workbook from scrapy.conf import settings import py ...

  4. vs2013 遇到的web性能记录器无法使用问题

    诊断和修复Web测试记录栏的问题.自2005年以来VSTS运也出现了各种由客户多年来提出不同的问题.记录Web测试时,这在一定程度经常提到的一个话题是一个残疾或不存在的Web测试记录吧.因为它可以令人 ...

  5. 关于allow_url_fopen的设置与服务器的安全

    allow_url_fopen与安全以及PHP libcurl allow_url_fopen=ON常常会给服务器和管理员带来麻烦,但是经常性(至少我这样认为)的我们需要远程读取某个东西,如果设置al ...

  6. Android Framework层Power键关机流程(一,Power长按键操作处理)

    一:Android处理Power按键长按操作 在Framework层中,Android4.x对Power键(KeyEvent.KEYCODE_POWER)的操作,我们从PhoneWindowManag ...

  7. MAC上python+Eclipse+pydev环境搭建

    转自:http://www.cnblogs.com/Bonker/p/3584707.html 本文重点介绍使用Eclipse+pydev插件来写Python代码,  以及在Mac上配置Eclipse ...

  8. REDIS key notification

    Commands Clients Documentation Community Download Support License Join us in London October 19th for ...

  9. js实现时间控件

    <html><head> <title>时间控件</title></head><body > <input name=&q ...

  10. 十分钟了解分布式计算:Petuum

    Petuum是一个机器学习专用分布式计算框架,本文介绍其架构,并基于文章 More Effective Distributed ML via a Stale Synchronous Parallel ...