springboot结合swagger生成接口文档
原文链接:https://www.cnblogs.com/xu-lei/p/7423883.html
https://www.jianshu.com/p/b9ae3136b292
前后台分离的开发渐渐已成趋势。那么前后端的沟通就成了问题,包括移动端,web端。如果有一个东西在我们写完代码的时候,自动将接口的所有注释,调用文档提供出来,是不是一件很美好的事情。那就是使用swagger.
1.使用swagger,首先在pom中引入jar依赖。
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger2</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>io.springfox</groupId>
<artifactId>springfox-swagger-ui</artifactId>
<version>2.2.2</version>
</dependency>
2.Application.java中引入@EnableSwagger2来启动swagger注解
@Configuration
@SpringBootApplication // 组件扫描
@EnableScheduling
@EnableAsync
@EnableSwagger2
public class Application {
3.配置swagger的基本信息
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import springfox.documentation.builders.ApiInfoBuilder;
import springfox.documentation.builders.PathSelectors;
import springfox.documentation.builders.RequestHandlerSelectors;
import springfox.documentation.service.ApiInfo;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket; /**
* @author 小尘哥
* api 地址:http://localhost:9090/swagger-ui.html
*/
@Configuration
public class SwaggerConfig { @Bean
public Docket createRestApi() {
return new Docket(DocumentationType.SWAGGER_2)
.apiInfo(apiInfo())
.select()
.apis(RequestHandlerSelectors.basePackage("com.mos.eboot.service"))
.paths(PathSelectors.any())
.build();
} private ApiInfo apiInfo() {
return new ApiInfoBuilder()
.title("eboot-api文档")
.description("更多信息,请访问https://www.jianshu.com/u/3979cb11f079")
.termsOfServiceUrl("https://gitee.com/QuanZhanZhiLu/easy-boot")
.version("1.0")
.build();
}
}
4.使用接口注解
@RestController
@RequestMapping("/user")
@Api("userController相关api")
public class UserController { @Autowired
private UserService userService; // @Autowired
// private MyRedisTemplate myRedisTemplate; @ApiOperation("获取用户信息")
@ApiImplicitParams({
@ApiImplicitParam(paramType="header",name="username",dataType="String",required=true,value="用户的姓名",defaultValue="zhaojigang"),
@ApiImplicitParam(paramType="query",name="password",dataType="String",required=true,value="用户的密码",defaultValue="wangna")
})
@ApiResponses({
@ApiResponse(code=400,message="请求参数没填好"),
@ApiResponse(code=404,message="请求路径没有或页面跳转路径不对")
})
@RequestMapping(value="/getUser",method=RequestMethod.GET)
public User getUser(@RequestHeader("username") String username, @RequestParam("password") String password) {
return userService.getUser(username,password);
} // @RequestMapping("/testJedisCluster")
// public User testJedisCluster(@RequestParam("username") String username){
// String value = myRedisTemplate.get(MyConstants.USER_FORWARD_CACHE_PREFIX, username);
// if(StringUtils.isBlank(value)){
// myRedisTemplate.set(MyConstants.USER_FORWARD_CACHE_PREFIX, username, JSON.toJSONString(getUser()));
// return null;
// }
// return JSON.parseObject(value, User.class);
// } }
说明:
- @Api:用在类上,说明该类的作用
- @ApiOperation:用在方法上,说明方法的作用
- @ApiImplicitParams:用在方法上包含一组参数说明
- @ApiImplicitParam:用在@ApiImplicitParams注解中,指定一个请求参数的各个方面
- paramType:参数放在哪个地方
- header-->请求参数的获取:@RequestHeader
- query-->请求参数的获取:@RequestParam
- path(用于restful接口)-->请求参数的获取:@PathVariable
- body(不常用)
- form(不常用)
- name:参数名
- dataType:参数类型
- required:参数是否必须传
- value:参数的意思
- defaultValue:参数的默认值
- paramType:参数放在哪个地方
- @ApiResponses:用于表示一组响应
- @ApiResponse:用在@ApiResponses中,一般用于表达一个错误的响应信息
- code:数字,例如400
- message:信息,例如"请求参数没填好"
- response:抛出异常的类
- @ApiModel:描述一个Model的信息(这种一般用在post创建的时候,使用@RequestBody这样的场景,请求参数无法使用@ApiImplicitParam注解进行描述的时候)
- @ApiModelProperty:描述一个model的属性
5.启动服务,浏览器输入"http://localhost:8080/swagger-ui.html"
springboot结合swagger生成接口文档的更多相关文章
- 【转】springboot结合swagger生成接口文档
前后台分离的开发渐渐已成趋势.那么前后端的沟通就成了问题,包括移动端,web端.如果有一个东西在我们写完代码的时候,自动将接口的所有注释,调用文档提供出来,是不是一件很美好的事情.那就是使用swagg ...
- asp.net core 使用 swagger 生成接口文档
参考地址:http://www.cnblogs.com/daxnet/p/6181366.html http://www.jianshu.com/p/fa5a9b76f3ed 微软参考文档:https ...
- webapi 利用webapiHelp和swagger生成接口文档
webapi 利用webapiHelp和swagger生成接口文档.均依赖xml(需允许项目生成注释xml) webapiHelp:微软技术自带,仅含有模块.方法.请求-相应参数的注释. swagge ...
- .net core 使用 swagger 生成接口文档
微软参考文档:https://docs.microsoft.com/en-us/aspnet/core/tutorials/web-api-help-pages-using-swagger?tabs= ...
- Springboot集成swagger2生成接口文档
[转载请注明]: 原文出处:https://www.cnblogs.com/jstarseven/p/11509884.html 作者:jstarseven 码字挺辛苦的..... 一 ...
- asp.net core使用Swashbuckle.AspNetCore(swagger)生成接口文档
asp.net core中使用Swashbuckle.AspNetCore(swagger)生成接口文档 Swashbuckle.AspNetCore:swagger的asp.net core实现 项 ...
- Django使用swagger生成接口文档
参考博客:Django接入Swagger,生成Swagger接口文档-操作解析 Swagger是一个规范和完整的框架,用于生成.描述.调用和可视化RESTful风格的Web服务.总体目标是使客户端和文 ...
- SpringBoot整合Swagger3生成接口文档
前后端分离的项目,接口文档的存在十分重要.与手动编写接口文档不同,swagger是一个自动生成接口文档的工具,在需求不断变更的环境下,手动编写文档的效率实在太低.与swagger2相比新版的swagg ...
- Go语言使用swagger生成接口文档
swagger介绍 Swagger本质上是一种用于描述使用JSON表示的RESTful API的接口描述语言.Swagger与一组开源软件工具一起使用,以设计.构建.记录和使用RESTful Web服 ...
随机推荐
- RabbitMQ介绍
(一)RabbitMQ基本概念 RabbitMQ是流行的开源消息队列系统,用erlang语言开发.我曾经对这门语言挺有兴趣,学过一段时间,后来没坚持.RabbitMQ是 AMQP(高级消息队列协议)的 ...
- centos7下搭建ceph luminous(12.2.1)--无网或网络较差
本博客的主要内容是在centos7下搭建luminous,配置dashboard,搭建客户端使用rbd,源码安装ceph,最后给出一些较为常用的命令.本博客针对初次接触ceph的人群. 搭建环境: 主 ...
- CRC 简介
CRC wiki,历史发展,各个版本的用途 等 https://en.wikipedia.org/wiki/Cyclic_redundancy_check (apple)crc32.c /* * Th ...
- app自动更新(android)
更新插件代码:https://github.com/shixy/UpdateApp 来源:http://aspoems.iteye.com/blog/1897300 检查更新的时候,通过指定的URL获 ...
- python学习笔记(二):python数据类型
上一篇博客写了python的入门和简单流程控制,这次写python的数据类型和各种数据类型的内置方法.一.数据类型是什么鬼?计算机顾名思义就是可以做数学计算的机器,因此,计算机程序理所当然地可以处理各 ...
- 阿里云服务器ubuntu安装redis2.8.13
阿里云服务器ubuntu安装redis2.8.13 2014-09-04 16:14 | coding云 | 2198次阅读 | 暂无评论 一.下载redis 可以先下载到本地,然后ftp到服 ...
- 解决springMVC文件上传报错: The current request is not a multipart request
转自:https://blog.csdn.net/HaHa_Sir/article/details/79131607 解决springMVC文件上传报错: The current request is ...
- Webrtc服务器搭建<转>
http://blog.csdn.net/zqf_office/article/details/49851209
- MongoDB中的查询
MongoDB中文文档:http://docs.mongoing.com/manual-zh/contents.html 这里以集合名称为test为例,数据库通过for循环插入一些测试数据,键分别为: ...
- ava的打包jar、war、ear包的作用、区别、打包方式
编为大家介绍,基于Java的打包jar.war.ear包的作用与区别详解.需要的朋友参考下以最终客户的角度来看,JAR文件就是一种封装,他们不需要知道jar文件中有多少个.class文件,每个文件中的 ...