本文参考第四章 springboot + swagger(官网http://swagger.io/support/)
一、使用swagger优点:
- API文档清楚简明
- 为了测试简便,做到前后端分离
- spring-boot与swagger集成简单
二、项目结构与第二章 springboot+mybatis中的一致
引入swagger的jar:pom.xml
1 <dependency> 2 <groupId>io.springfox</groupId> 3 <artifactId>springfox-swagger2</artifactId> 4 <version>2.2.2</version> 5 </dependency> 6 <dependency> 7 <groupId>io.springfox</groupId> 8 <artifactId>springfox-swagger-ui</artifactId> 9 <version>2.2.2</version> 10 </dependency>
三、在WeiboApplication.java中加入@EnableSwagger2注解
1 package com.weibo2; 2 3 import org.springframework.boot.SpringApplication; 4 import org.springframework.boot.autoconfigure.SpringBootApplication; 5 6 import springfox.documentation.swagger2.annotations.EnableSwagger2; 7 8 @SpringBootApplication 9 @EnableSwagger2 10 public class WeiboApplication { 11 12 public static void main(String[] args) { 13 SpringApplication.run(WeiboApplication.class, args); 14 } 15 }
说明:@EnableSwagger2用来启动Swagger,加入该注解使得用在controller中的swagger注解生效,覆盖的范围由@ComponentScan的配置来指定,
这里默认指定为根路径"com.weibo2"下的所有controller)
四、WeiboController.java
1 package com.weibo2.controller; 2 3 import java.util.Date; 4 5 import org.springframework.beans.factory.annotation.Autowired; 6 import org.springframework.web.bind.annotation.RequestMapping; 7 import org.springframework.web.bind.annotation.RequestMethod; 8 import org.springframework.web.bind.annotation.RequestParam; 9 import org.springframework.web.bind.annotation.RestController; 10 11 import com.weibo2.model.Weibo; 12 import com.weibo2.service.WeiboService; 13 14 import io.swagger.annotations.Api; 15 import io.swagger.annotations.ApiImplicitParam; 16 import io.swagger.annotations.ApiImplicitParams; 17 import io.swagger.annotations.ApiOperation; 18 import io.swagger.annotations.ApiResponse; 19 import io.swagger.annotations.ApiResponses; 20 21 @Api("微博相关API") 22 @RestController 23 @RequestMapping("/weibo") 24 public class WeiboController { 25 @Autowired 26 private WeiboService weiboService; 27 28 @ApiOperation("发表微博") 29 @ApiImplicitParams({ 30 @ApiImplicitParam(paramType = "query", name = "content", value = "微博内容", dataType = "String", required = true), 31 @ApiImplicitParam(paramType = "query", name = "owner", value = "发表者", dataType = "String", required = true) }) 32 @RequestMapping(value = "/addWeibo", method = RequestMethod.POST) 33 @ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), 34 @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对") }) 35 public boolean addWeibo(@RequestParam("content") String content, @RequestParam("owner") String owner) { 36 Weibo weibo = new Weibo(); 37 weibo.setContent(content); 38 weibo.setOwner(owner); 39 weibo.setCreatetime(new Date()); 40 weibo.setLastmodifytime(new Date()); 41 weibo.setReadcount(0); 42 return weiboService.addWeibo(weibo); 43 } 44 45 @ApiOperation("查询微博") 46 @ApiImplicitParams({ 47 @ApiImplicitParam(paramType = "query", name = "id", value = "微博ID", dataType = "Integer", required = true) }) 48 @ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), 49 @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对") }) 50 @RequestMapping(value = "/getWeibo", method = RequestMethod.GET) 51 public Weibo getWeibo(@RequestParam("id") Integer id) { 52 return weiboService.getWeibo(id); 53 } 54 55 @ApiOperation("更新微博") 56 @ApiImplicitParams({ 57 @ApiImplicitParam(paramType = "query", name = "id", value = "微博ID", dataType = "Integer", required = true), 58 @ApiImplicitParam(paramType = "query", name = "content", value = "微博内容", dataType = "String", required = false), 59 @ApiImplicitParam(paramType = "query", name = "owner", value = "发表者", dataType = "String", required = false) }) 60 @ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), 61 @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对") }) 62 @RequestMapping(value = "/updateWeibo", method = RequestMethod.PUT) 63 public boolean updateWeibo(@RequestParam(value = "id", required = true) Integer id, 64 @RequestParam(value = "content", required = false) String content, 65 @RequestParam(value = "owner", required = false) String owner) { 66 Weibo weibo = weiboService.getWeibo(id); 67 weibo.setContent(content); 68 weibo.setOwner(owner); 69 return weiboService.updateWeibo(weibo); 70 } 71 72 @ApiOperation("删除微博") 73 @ApiImplicitParams({ 74 @ApiImplicitParam(paramType = "query", name = "id", value = "微博ID", dataType = "String", required = true) }) 75 @ApiResponses({ @ApiResponse(code = 400, message = "请求参数没填好"), 76 @ApiResponse(code = 404, message = "请求路径没有或页面跳转路径不对") }) 77 @RequestMapping(value = "/deleteWeibo", method = RequestMethod.DELETE) 78 public boolean deleteWeibo(@RequestParam("id") Integer id) { 79 return weiboService.deleteWeibo(id); 80 } 81 }
说明:
- @API:用于类上,使该类成为Swagger resource.eg:@Api(value = "/pet", description = "Operations about pets")
- @ApiOperation:用于方法上,描述方法作用
-
@ApiOperation(value = "Finds Pets by status", notes = "Multiple status values can be provided with comma seperated strings", response = Pet.class, responseContainer = "List") public Response findPetsByStatus(...) { ... }
- @ApiResponses:用于表示一组响应
- @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
- code:数字,例如400
- message:信息,例如"请求参数没填好"
- response:抛出异常的类
-
@ApiImplicitParams:用于方法上,包含一组参数
- @ApiImplicitParam:用于@ApiImplicitParams中,具体说明参数
- name:参数名
- dataType:参数类型
- required:参数是否必须传
- value:参数的意思
- defaultValue:参数的默认值
- paramType-->参数位置
- header-->请求头,请求参数的获取:@RequestHeader
- query-->请求体,请求参数的获取:@RequestParam
- path(用于restful接口)-->请求路径,请求参数的获取:@PathVariable
- body(不常用)
- form(不常用)
五、测试
在浏览器中输入:http://localhost:8080/swagger-ui.html
说明:
- 微博相关api-->@API
- 发表微博、删除微博。。-->@ApiOperation
- content(owner)-->@ApiImplicitParam
- 400-->@ApiResponse