SpringBoot + SwaggerUI
2018-08-06 14:28
405 查看
后台写接口,由于要提供接口文档给前台使用,所有研究了一下swagger,看到网上有篇文章写得不错,就直接拿过来了。
swagger用于定义API文档。
好处:
- 前后端分离开发
- API文档非常明确
- 测试的时候不需要再使用URL输入浏览器的方式来访问Controller
- 传统的输入URL的测试方式对于post请求的传参比较麻烦(当然,可以使用postman这样的浏览器插件)
- spring-boot与swagger的集比较成简单
1、项目结构
和上一节一样,没有改变。
2、pom.xml
引入了两个jar。
3、Application.java
说明:
引入了一个注解@EnableSwagger24、UserController.java
说明:
- @Api:用在类上,说明该类的作用
- @ApiOperation:用在方法上,说明方法的作用
- @ApiImplicitParams:用在方法上包含一组参数说明
- @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面 paramType:参数放在哪个地方 header-->请求参数的获取:@RequestHeader
- query-->请求参数的获取:@RequestParam
- path(用于restful接口)-->请求参数的获取:@PathVariable
- body(不常用)
- form(不常用)
-
code:数字,例如400
-
@ApiModelProperty:描述一个model的属性
以上这些就是最常用的几个注解了。
需要注意的是:
- ApiImplicitParam这个注解不只是注解,还会影响运行期的程序,例子如下:
如果ApiImplicitParam中的phone的paramType是query的话,是无法注入到rest路径中的,而且如果是path的话,是不需要配置ApiImplicitParam的,即使配置了,其中的value="手机号"也不会在swagger-ui展示出来。
具体其他的注解,查看:
https://github.com/swagger-api/swagger-core/wiki/Annotations#apimodel
测试:
启动服务,浏览器输入"http://localhost:8080/swagger-ui.html"
最上边一个红框:@Api
GET红框:method=RequestMethod.GET
右边红框:@ApiOperation
parameter红框:@ApiImplicitParams系列注解
response messages红框:@ApiResponses系列注解
输入参数后,点击"try it out!",查看响应内容:
转载----https://www.cnblogs.com/gavin-liu/p/6491545.html
相关文章推荐
- 多种方式解决spring boot swagger ui使用 nginx 部署后无法使用问题
- Swagger使用及Springfox+SpringBoot访问http://localhost:8080/swagger-ui.htmlui404
- Spring boot+Shiro+ spring MVC+swagger UI +Mybatis+mysql+Vue +Element UI 之四 vue 基本知识点概述
- SpringBoot + SwaggerUI
- 在springboot项目中引入swagger2时出现Circular view path [swagger-ui]
- springboot+swaggerui配置
- 解决spring boot中swagger-ui.html访问404以及配置全局header
- Spring boot+Shiro+ spring MVC+swagger UI +Mybatis+mysql+Vue +Element UI 之三 vue简单页面实战
- Spring boot 中使用swagger-ui实现 restful-api
- Spring boot+Shiro+ spring MVC+swagger UI +Mybatis+mysql+Vue +Element UI 之二 vue 环境演示
- Swagger-ui与springBoot的结合
- 在Spring Boot中使用swagger-bootstrap-ui
- 在Spring Boot中使用swagger-bootstrap-ui的方法
- SwaggerUI自动生成API文档(SwaggerUI+SpringBoot)
- Spring boot+Shiro+ spring MVC+swagger UI +Mybatis+mysql+Vue +Element UI 之一vue和spring boot整合
- Spring boot+Shiro+ spring MVC+swagger UI +Mybatis+mysql+Vue +Element UI 之四 vue 整合Element UI
- 在Spring Boot中使用swagger-bootstrap-ui
- SwaggerUI+SpringMVC-构建RestFul API的可视化界面
- springboot swagger starter
- springboot (spring mvc)集成swagger