因为Spring Boot可以快速开发、便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API。而咱们构建RESTful API的目的一般都是因为多终端的缘由,这些终端会共用不少底层业务逻辑,所以咱们会抽象出这样一层来同时服务于多个移动端或者Web前端。html
这样一来,咱们的RESTful API就有可能要面对多个开发人员或多个开发团队:IOS开发、Android开发或是Web开发等。为了减小与其余团队平时开发期间的频繁沟通成本,传统作法咱们会建立一份RESTful API文档来记录全部接口细节,然而这样的作法有如下几个问题:前端
为了解决上面这样的问题,本文将介绍RESTful API的重磅好伙伴Swagger2,它能够轻松的整合到Spring Boot中,并与Spring MVC程序配合组织出强大RESTful API文档。它既能够减小咱们建立文档的工做量,同时说明内容又整合入实现代码中,让维护文档和修改代码整合为一体,可让咱们在修改代码逻辑的同时方便的修改文档说明。另外Swagger2也提供了强大的页面测试功能来调试每一个RESTful API。具体效果以下图所示:java
下面来具体介绍,若是在Spring Boot中使用Swagger2。首先,咱们须要一个Spring Boot实现的RESTful API工程,若您没有作过这类内容,建议先阅读
Spring Boot构建一个较为复杂的RESTful APIs和单元测试。web
下面的内容咱们会以教程样例中的Chapter3-1-1进行下面的实验(Chpater3-1-5是咱们的结果工程,亦可参考)。spring
在pom.xml
中加入Swagger2的依赖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>
在Application.java
同级建立Swagger2的配置类Swagger2
。springboot
@Configuration @EnableSwagger2 public class Swagger2 { @Bean public Docket createRestApi() { return new Docket(DocumentationType.SWAGGER_2) .apiInfo(apiInfo()) .select() .apis(RequestHandlerSelectors.basePackage("com.didispace.web")) .paths(PathSelectors.any()) .build(); } private ApiInfo apiInfo() { return new ApiInfoBuilder() .title("Spring Boot中使用Swagger2构建RESTful APIs") .description("更多Spring Boot相关文章请关注:http://blog.didispace.com/") .termsOfServiceUrl("http://blog.didispace.com/") .contact("程序猿DD") .version("1.0") .build(); } }
再经过createRestApi
函数建立Docket
的Bean以后,apiInfo()
用来建立该Api的基本信息(这些基本信息会展示在文档页面中)。select()
函数返回一个ApiSelectorBuilder
实例用来控制哪些接口暴露给Swagger来展示,本例采用指定扫描的包路径来定义,Swagger会扫描该包下全部Controller定义的API,并产生文档内容(除了被@ApiIgnore
指定的请求)。如上代码所示,经过@Configuration
注解,让Spring来加载该类配置。再经过@EnableSwagger2
注解来启用Swagger2。restful
@RestController @RequestMapping(value="/users") // 经过这里配置使下面的映射都在/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
注解来给API增长说明、经过@ApiImplicitParams
、@ApiImplicitParam
注解来给参数增长说明。数据结构
alt完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html
。就能看到前文所展现的RESTful API的页面。咱们能够再点开具体的API请求,以POST类型的/users请求为例,可找到上述代码中咱们配置的Notes信息以及参数user的描述信息,以下图所示。
在上图请求的页面中,咱们看到user的Value是个输入框?是的,Swagger除了查看接口功能外,还提供了调试测试功能,咱们能够点击上图中右侧的Model Schema(黄色区域:它指明了User的数据结构),此时Value中就有了user对象的模板,咱们只须要稍适修改,点击下方“Try it out!”
按钮,便可完成了一次请求调用!
此时,你也能够经过几个GET请求来验证以前的POST请求是否正确。
相比为这些接口编写文档的工做,咱们增长的配置内容是很是少并且精简的,对于原有代码的侵入也在忍受范围以内。所以,在构建RESTful API的同时,加入swagger来对API文档进行管理,是个不错的选择。
本文转载自 程序猿DD-翟永超:http://blog.didispace.com/springbootswagger2/