创建Swagger2配置类
在Application.java
同级创建Swagger2的配置类Swagger2
。
1 @Configuration
2 @EnableSwagger2
3 public class Swagger2 {
4 @Bean
5 public Docket createRestApi() {
6 return new Docket(DocumentationType.SWAGGER_2)
7 .apiInfo(apiInfo())
8 .select()
9 .apis(RequestHandlerSelectors.basePackage("com.didispace.web"))
10 .paths(PathSelectors.any())
11 .build();
12 }
13 private ApiInfo apiInfo() {
14 return new ApiInfoBuilder()
15 .title("Spring Boot中使用Swagger2构建RESTful APIs")
16 .description("更多Spring Boot相关文章请关注:http://blog.didispace.com/")
17 .termsOfServiceUrl("http://blog.didispace.com/")
18 .contact("程序猿DD")
19 .version("1.0")
20 .build();
21 }
22 }
如上代码所示,通过@Configuration
注解,让Spring来加载该类配置。再通过@EnableSwagger2
注解来启用Swagger2。
再通过createRestApi
函数创建Docket
的Bean之后,apiInfo()
用来创建该Api的基本信息(这些基本信息会展现在文档页面中)。select()
函数返回一个ApiSelectorBuilder
实例用来控制哪些接口暴露给Swagger来展现,本例采用指定扫描的包路径来定义,Swagger会扫描该包下所有Controller定义的API,并产生文档内容(除了被@ApiIgnore
指定的请求)。
添加文档内容
在完成了上述配置后,其实已经可以生产文档内容,但是这样的文档主要针对请求本身,而描述主要来源于函数等命名产生,对用户并不友好,我们通常需要自己增加一些说明来丰富文档内容。如下所示,我们通过@ApiOperation
注解来给API增加说明、通过@ApiImplicitParams
、@ApiImplicitParam
注解来给参数增加说明。
1 @RestController
2 @RequestMapping(value="/users") // 通过这里配置使下面的映射都在/users下,可去除
3 public class UserController {
4 static Map users = Collections.synchronizedMap(new HashMap());
5 @ApiOperation(value="获取用户列表", notes="")
6 @RequestMapping(value={""}, method=RequestMethod.GET)
7 public List getUserList() {
8 List r = new ArrayList(users.values());
9 return r;
10 }
11 @ApiOperation(value="创建用户", notes="根据User对象创建用户")
12 @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
13 @RequestMapping(value="", method=RequestMethod.POST)
14 public String postUser(@RequestBody User user) {
15 users.put(user.getId(), user);
16 return "success";
17 }
18 @ApiOperation(value="获取用户详细信息", notes="根据url的id来获取用户详细信息")
19 @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
20 @RequestMapping(value="/{id}", method=RequestMethod.GET)
21 public User getUser(@PathVariable Long id) {
22 return users.get(id);
23 }
24 @ApiOperation(value="更新用户详细信息", notes="根据url的id来指定更新对象,并根据传过来的user信息来更新用户详细信息")
25 @ApiImplicitParams({
26 @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long"),
27 @ApiImplicitParam(name = "user", value = "用户详细实体user", required = true, dataType = "User")
28 })
29 @RequestMapping(value="/{id}", method=RequestMethod.PUT)
30 public String putUser(@PathVariable Long id, @RequestBody User user) {
31 User u = users.get(id);
32 u.setName(user.getName());
33 u.setAge(user.getAge());
34 users.put(id, u);
35 return "success";
36 }
37 @ApiOperation(value="删除用户", notes="根据url的id来指定删除对象")
38 @ApiImplicitParam(name = "id", value = "用户ID", required = true, dataType = "Long")
39 @RequestMapping(value="/{id}", method=RequestMethod.DELETE)
40 public String deleteUser(@PathVariable Long id) {
41 users.remove(id);
42 return "success";
43 }
44 }
完成上述代码添加上,启动Spring Boot程序,访问:http://localhost:8080/swagger-ui.html
。就能看到前文所展示的RESTful API的页面。我们可以再点开具体的API请求,以POST类型的/users请求为例,可找到上述代码中我们配置的Notes信息以及参数user的描述信息,如下图所示。
API文档访问与调试
在上图请求的页面中,我们看到user的Value是个输入框?是的,Swagger除了查看接口功能外,还提供了调试测试功能,我们可以点击上图中右侧的Model Schema(黄色区域:它指明了User的数据结构),此时Value中就有了user对象的模板,我们只需要稍适修改,点击下方“Try it out!”
按钮,即可完成了一次请求调用!
此时,你也可以通过几个GET请求来验证之前的POST请求是否正确。
相比为这些接口编写文档的工作,我们增加的配置内容是非常少而且精简的,对于原有代码的侵入也在忍受范围之内。因此,在构建RESTful API的同时,加入swagger来对API文档进行管理,是个不错的选择。
完整结果示例可查看Chapter3-1-5。