博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
第三章 springboot+swagger
阅读量:4348 次
发布时间:2019-06-07

本文共 6087 字,大约阅读时间需要 20 分钟。

 本文参考(官网http://swagger.io/support/)

一、使用swagger优点:

  • API文档清楚简明
  • 为了测试简便,做到前后端分离
  • spring-boot与swagger集成简单

二、项目结构与中的一致

引入swagger的jar:pom.xml

1 
2
io.springfox
3
springfox-swagger2
4
2.2.2
5
6
7
io.springfox
8
springfox-swagger-ui
9
2.2.2
10
View Code

三、在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 @EnableSwagger210 public class WeiboApplication {11 12     public static void main(String[] args) {13         SpringApplication.run(WeiboApplication.class, args);14     }15 }
View Code

说明:@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 @RestController23 @RequestMapping("/weibo")24 public class WeiboController {25     @Autowired26     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 }
View Code

说明:

  • @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(...) { ... }
    View Code
  • @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

 

转载于:https://www.cnblogs.com/wangna----558169/p/6187230.html

你可能感兴趣的文章
maven私服nexus3.9安装配置
查看>>
U盘出现大量乱码文件,并且不能彻底删除
查看>>
UEditor添加一个普通按钮及其他使用注意事项
查看>>
C语言的第一次实验报告
查看>>
spring JDBC 批量插入数据
查看>>
状态压缩题目小结
查看>>
Android WebView 开发具体解释(三)
查看>>
2016-2017-2 20155325实验二《Java面向对象程序设计》实验报告
查看>>
POJ.3145.Common Substrings(后缀数组 倍增 单调栈)
查看>>
BZOJ.1935.[SHOI2007]Tree园丁的烦恼(CDQ分治 三维偏序)
查看>>
c++可变参数(示例)
查看>>
4923: [Lydsy1706月赛]K小值查询 平衡树 非旋转Treap
查看>>
MySQL 配置文件详解
查看>>
maven 中央仓库地址
查看>>
实现页面查看xml或json数据类似控制台效果
查看>>
Kali配置教程
查看>>
Leetcode: Combination Sum IV && Summary: The Key to Solve DP
查看>>
获取动态类型变量的属性值
查看>>
C++冒泡排序
查看>>
js 数组操作
查看>>