springboot06-swagger2 自动化api文档
1.springboot 项目中添加swagger2依赖:
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency> <dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
2.启动类中添加swagger注解支持:
@SpringBootApplication
@EnableSwagger2 //启动swagger注解 启动服务,浏览器输入"http://服务名:8080/swagger-ui.html"
@ComponentScan(basePackages = {"com.mlxs.springboot06.swagger"})
public class MainApp {
public static void main(String[] args) {
SpringApplication.run(MainApp.class, args);
}
}
3.自定义接口中填写api文档信息:
package com.mlxs.springboot06.swagger; import com.mlxs.springboot06.swagger.bean.User;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController; import java.util.ArrayList;
import java.util.List; /**
* UserResource类描述:
*
* swagger2使用说明:
@Api:用在类上,说明该类的作用
@ApiOperation:用在方法上,说明方法的作用
@ApiImplicitParams:用在方法上包含一组参数说明
@ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
paramType:参数放在哪个地方
header-->请求参数的获取:@RequestHeader
query-->请求参数的获取:@RequestParam
path(用于restful接口)-->请求参数的获取:@PathVariable
body(不常用)
form(不常用)
name:参数名
dataType:参数类型
required:参数是否必须传
value:参数的意思
defaultValue:参数的默认值
@ApiResponses:用于表示一组响应
@ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
code:数字,例如400
message:信息,例如"请求参数没填好"
response:抛出异常的类
@ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
@ApiModelProperty:描述一个model的属性
*
* @author yangzhenlong
* @since 2017/2/20
*/
@RestController
@RequestMapping("/user")
public class UserResource { @ApiOperation(value = "用户列表", httpMethod = "GET")
@RequestMapping(value = "/list", method = {RequestMethod.GET})
public List<User> userList(){
List<User> userList = new ArrayList<>();
for (int i=1; i<= 5; i++){
User user = new User(i, "用户" + i);
userList.add(user);
}
return userList;
} @ApiOperation(value = "根据Id获取用户信息", httpMethod = "GET")
@ApiImplicitParam(paramType = "query", name = "id", required = true, value = "用户id", defaultValue = "1")
@RequestMapping(value = "/get", method = {RequestMethod.GET})
public User userList(Integer id){
List<User> userList = new ArrayList<>();
for (int i=1; i<= 5; i++){
User user = new User(i, "用户" + i);
userList.add(user);
}
return userList.get(id -1);
}
}
4.启动MainApp类,浏览器访问http://localhost:8080/swagger-ui.html 查看效果:
springboot06-swagger2 自动化api文档的更多相关文章
- 白话SpringCloud | 第十一章:路由网关(Zuul):利用swagger2聚合API文档
前言 通过之前的两篇文章,可以简单的搭建一个路由网关了.而我们知道,现在都奉行前后端分离开发,前后端开发的沟通成本就增加了,所以一般上我们都是通过swagger进行api文档生成的.现在由于使用了统一 ...
- Spring Boot中使用Swagger2构建API文档
程序员都很希望别人能写技术文档,自己却很不愿意写文档.因为接口数量繁多,并且充满业务细节,写文档需要花大量的时间去处理格式排版,代码修改后还需要同步修改文档,经常因为项目时间紧等原因导致文档滞后于代码 ...
- 使用springfox+swagger2书写API文档(十八)
使用springfox+swagger2书写API文档 springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfo ...
- SpringBoot+rest接口+swagger2生成API文档+validator+mybatis+aop+国际化
代码地址:JillWen_SpringBootDemo mybatis 1. 添加依赖: <dependency> <groupId>org.mybatis.spring.bo ...
- SpringBoot中使用springfox+swagger2书写API文档
随着前后端的分离,借口文档变的尤其重要,springfox是通过注解的形式自动生成API文档,利用它,可以很方便的书写restful API,swagger主要用于展示springfox生成的API文 ...
- Spring Boot 整合Swagger2构建API文档
1.pom.xml中引入依赖 <dependency> <groupId>io.springfox</groupId> <artifactId>spri ...
- springboot + swagger2 生成api文档
直接贴代码: <dependency> <groupId>io.springfox</groupId> <artifactId>springfox-sw ...
- Spring Boot 2.X(十五):集成 Swagger2 开发 API 文档(在线+离线)
前言 相信很多后端开发在项目中都会碰到要写 api 文档,不管是给前端.移动端等提供更好的对接,还是以后为了以后交接方便,都会要求写 api 文档. 而手写 api 文档的话有诸多痛点: 文档更新的时 ...
- SpringBoot使用Swagger2构建API文档
后端开发中经常需要对移动客户端提供RESTful API接口,在后期版本快速迭代的过程中,修改接口实现的时候都必须同步修改接口文档,而文档与代码又处于两个不同的媒介,除非有严格的管理机制,不然很容易导 ...
随机推荐
- NOIP2013火柴排队
Solution 恕我直言,这题是真的坑. 对于这道题,一个很显然的思路是对于A B两个序列,他们交换完后相对的两个数在原序列中的相对大小是相同的,于是我们就把序列按照A排序,在把B离散化,求逆序对, ...
- vue-router 如何默认显示三级子路由
{ path: '/aaa', name: 'aaa', title: '统计分析', component: () => import('@/aaa.vue'),//一级子组件.容器 child ...
- Python中pandas dataframe删除一行或一列:drop函数
用法:DataFrame.drop(labels=None,axis=0, index=None, columns=None, inplace=False) 参数说明:labels 就是要删除的行列的 ...
- 利用sqlalchemy读取数据库 和pandas的Dataframe对象 互相生成
#导入pandas import pandas as pd import numpy as np #导入SqlAlchemy from sqlalchemy import create_engine ...
- XTest
腾讯优测是一个移动云测试平台,为应用.游戏.H5混合应用的研发团队提供产品质量检测与问题解决服务. 这是腾讯内部针对微信内的H5,做了一套专门的UI自动化框架.而且都是用真机来跑这些框架,在真机上模拟 ...
- 团体程序设计天梯赛(CCCC) L3012 水果忍者 上凸或下凹的证明
团体程序设计天梯赛代码.体现代码技巧,比赛技巧. https://github.com/congmingyige/cccc_code #include <cstdio> #include ...
- Solr7.1--- 单机Linux环境搭建
应网友的要求,写个关于Linux单机的 首先,把MySQL驱动包和solr7.1安装包上传到服务器,我上传到了自定义的目录/mysoft 执行服务安装脚本 1. 先切换到root用户 2. 解压出脚本 ...
- Codeforces Round #525 (Div. 2) D. Ehab and another another xor problem(待完成)
参考资料: [1]:https://blog.csdn.net/weixin_43790474/article/details/84815383 [2]:http://www.cnblogs.com/ ...
- [Discuz!] Discuz X1.5点击“发帖”出现XML代码的解决办法!
使用的是Discuz X1.5程序,不知什么原因,今天突然出现了大问题,就是在点击“发帖”的时候,原来正常显示的网页竟然变成了XML代码!经过一番查找资料,也未能找到是什么原因导致的,只是找到了解决办 ...
- QImage与cv::Mat转换;
QImage主要格式有QImage::Format_RGB32, QImage::Format_RGB888, QImage::Format_Index8, 不同的格式有不同的排布: 格式部分可以参考 ...