swagger2是一个用于生成、并能直接调用的可是话restful风格的服务

下面贴出springboot整合swagger2代码

一、maven依赖

这里使用的spring-boot版本是2.1.1.RELEASE,swagger2使用的是2.9.2,我一开始用的springboot1.5.6.RELEASE,swagger从2.4到2.9.2都用了,结果又很多jar包冲突,springboot换成了2.*版本依旧,依然有jar冲突,无奈,只能把冲突的依赖找出来,有具体报错信息,把他exclude掉就行,而且idea的maven-helper插件帮助也蛮大

  1. <!-- swagger -->
  2. <dependency>
  3. <groupId>io.springfox</groupId>
  4. <artifactId>springfox-swagger2</artifactId>
  5. <version>2.9.2</version>
  6. <exclusions>
  7. <exclusion>
  8. <artifactId>spring-context</artifactId>
  9. <groupId>org.springframework</groupId>
  10. </exclusion>
  11. <exclusion>
  12. <artifactId>spring-beans</artifactId>
  13. <groupId>org.springframework</groupId>
  14. </exclusion>
  15. <exclusion>
  16. <artifactId>spring-aop</artifactId>
  17. <groupId>org.springframework</groupId>
  18. </exclusion>
  19. </exclusions>
  20. </dependency>
  21. <!-- swagger-ui -->
  22. <dependency>
  23. <groupId>io.springfox</groupId>
  24. <artifactId>springfox-swagger-ui</artifactId>
  25. <version>2.9.2</version>
  26. </dependency>

二、swagger2配置

  1. package com.hy.other.config;
  2.  
  3. import org.springframework.context.annotation.Bean;
  4. import org.springframework.context.annotation.Configuration;
  5. import springfox.documentation.builders.ApiInfoBuilder;
  6. import springfox.documentation.builders.PathSelectors;
  7. import springfox.documentation.builders.RequestHandlerSelectors;
  8. import springfox.documentation.service.ApiInfo;
  9. import springfox.documentation.service.Contact;
  10. import springfox.documentation.spi.DocumentationType;
  11. import springfox.documentation.spring.web.plugins.Docket;
  12. import springfox.documentation.swagger2.annotations.EnableSwagger2;
  13.  
  14. @Configuration
  15. @EnableSwagger2
  16. public class SwaggerConfiguration {
  17.  
  18. @Bean
  19. public Docket createRestApi() {
  20. return new Docket(DocumentationType.SWAGGER_2)
  21. .apiInfo(apiInfo())
  22. .select()
  23. // 生成api的包路径(一般是我们controller包的路径)
  24. .apis(RequestHandlerSelectors.basePackage("com.hy.other.controller"))
  25. .paths(PathSelectors.any())
  26. .build();
  27. }
  28.  
  29. // swagger ui 页面里面的一些信息
  30. private ApiInfo apiInfo() {
  31. return new ApiInfoBuilder()
  32. // 页面标题
  33. .title("spring boot swagger")
  34. // 创建人
  35. .contact(new Contact("hy", "https://www.cnblogs.com/xhy-shine", ""))
  36. // 版本号
  37. .version("1.0")
  38. // 描述
  39. .description("API接口文档")
  40. .build();
  41. }
  42. }

注意:@EnableSwagger2一定要加上,不然会无法访问swagger的ui界面,会报如下错误

三、代码(主要是swagger2的注解)

注解有挺多的,比较常用的就是@Api、@ApiOperation、@ApiImplicitParam、@ApiModel、@ApiModelProperty

@Api:包括下面的所有接口,有点类注释的意思

@ApiOperation:给接口增加说明

@ApiImplicitParams、@ApiImplicitParam:给接口参数添加说明

@ApiModel:描述对象类型的请求参数

@ApiModelProperty:描述对象的属性

注意:@ApiModelProperty注解的参数说明

  paramType:指定参数放在哪个地方(header:request header中,使用@RequestHeader获取;query:使用@RequestParam获取;path:使用@PathVariable获取;body、form两者不常用)可参考:https://swagger.io/docs/specification/describing-parameters/

  name:参数名

  dataType:参数类型

  required:是否必须

  value:参数的意思

  defaultValue:默认值

  1. package com.hy.other.controller;
  2.  
  3. import io.swagger.annotations.Api;
  4. import io.swagger.annotations.ApiImplicitParam;
  5. import io.swagger.annotations.ApiOperation;
  6. import org.springframework.amqp.rabbit.core.RabbitTemplate;
  7. import org.springframework.beans.factory.annotation.Autowired;
  8. import org.springframework.beans.factory.annotation.Value;
  9. import org.springframework.web.bind.annotation.RequestBody;
  10. import org.springframework.web.bind.annotation.RequestMapping;
  11. import org.springframework.web.bind.annotation.RequestParam;
  12. import org.springframework.web.bind.annotation.RestController;
  13.  
  14. @Api(description = "MQ接口")
  15. @RestController
  16. @RequestMapping("/messageQueue")
  17. public class MessageQueueController {
  18.  
  19. @Value("${rabbitmq.exchangeName}")
  20. private String exchangeName;
  21. @Value("${rabbitmq.queueName}")
  22. private String queueName;
  23. @Value("${rabbitmq.routeKey}")
  24. private String routeKey;
  25.  
  26. @Autowired
  27. private RabbitTemplate rabbitTemplate;
  28.  
  29. @ApiOperation(value = "send message to spin", notes = "发送消息到spin")
  30. @ApiImplicitParam(name = "message", value = "消息内容", required = true, paramType = "query")
  31. @RequestMapping("/sendWso2ToSpin")
  32. public String sendWso2ToSpin(@RequestParam("message") String message) {
  33. rabbitTemplate.convertAndSend(exchangeName, routeKey, message);
  34. return message;
  35. }
  36.  
  37. }

访问地址你的项目根路径加上swagger-ui.html就行

1、swagger会根据@RequestParam、@RequestBody注解来决定用form提交还是application/json格式(上图出现Parameter content type为application/json是因为我把请求参数前的注解改成了@RequestBody)

2、会根据@RequestMapping、@PostMapping、@GetMapping等注解生成对应的请求,如上,我写的@RequestMapping,他把所有请求格式都生成了

3、如果接收的是对象,可以使用@ApiModel结合@ApiModelProperty

SpringBoot-整合Swagger2的更多相关文章

  1. SpringBoot(七):SpringBoot整合Swagger2

    原文地址:https://blog.csdn.net/saytime/article/details/74937664 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文 ...

  2. SpringBoot整合Swagger2

    相信各位在公司写API文档数量应该不少,当然如果你还处在自己一个人开发前后台的年代,当我没说,如今为了前后台更好的对接,还是为了以后交接方便,都有要求写API文档. 手写Api文档的几个痛点: 文档需 ...

  3. SpringBoot整合Swagger2(Demo示例)

    写在前面 由于公司项目采用前后端分离,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理 ...

  4. springboot 整合Swagger2的使用

    Swagger2相较于传统Api文档的优点 手写Api文档的几个痛点: 文档需要更新的时候,需要再次发送一份给前端,也就是文档更新交流不及时. 接口返回结果不明确 不能直接在线测试接口,通常需要使用工 ...

  5. SpringBoot整合Swagger2案例,以及报错:java.lang.NumberFormatException: For input string: ""原因和解决办法

    原文链接:https://blog.csdn.net/weixin_43724369/article/details/89341949 SpringBoot整合Swagger2案例 先说SpringB ...

  6. SpringBoot整合Swagger2详细教程

    1. 简介   随着前后端分离开发模式越来越流行,编写接口文档变成了开发人员非常头疼的事.而Swagger是一个规范且完整的web框架,用于生成.描述.调用可视化的RESTful风格的在线接口文档,并 ...

  7. SpringBoot整合Swagger2及使用

    简介 swagger是一个流行的API开发框架,这个框架以"开放API声明"(OpenAPI Specification,OAS)为基础, 对整个API的开发周期都提供了相应的解决 ...

  8. SpringBoot整合Swagger2,再也不用维护接口文档了!

    前后端分离后,维护接口文档基本上是必不可少的工作.一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却很 ...

  9. SpringBoot学习笔记(16)----SpringBoot整合Swagger2

    Swagger 是一个规范和完整的框架,用于生成,描述,调用和可视化RESTful风格的web服务 http://swagger.io Springfox的前身是swagger-springmvc,是 ...

  10. Spring Boot2 系列教程(十七)SpringBoot 整合 Swagger2

    前后端分离后,维护接口文档基本上是必不可少的工作. 一个理想的状态是设计好后,接口文档发给前端和后端,大伙按照既定的规则各自开发,开发好了对接上了就可以上线了.当然这是一种非常理想的状态,实际开发中却 ...

随机推荐

  1. 014-数据结构-树形结构-基数树、Patricia树、默克尔树、梅克尔帕特里夏树( Merkle Patricia Tree, MPT)

    一.基数树 Radix树,即基数树,也称压缩前缀树,是一种提供key-value存储查找的数据结构.与Trie不同的是,它对Trie树进行了空间优化,只有一个子节点的中间节点将被压缩.同样的,Radi ...

  2. DevOps - DevOps工具链

    不要满足于使用世界上已存在的各种方法和技术,而应重点放在希望达到的效果上! 单单使用工具不难,困难的是在团队开发中熟练使用,并形成一套理想的工作流程,只有在团队中工具和思想才能发挥最大价值. 开源工具 ...

  3. 一、Node.js安装及环境配置之Windows篇

    一.安装环境 1.本机系统:Windows 10 Pro(64位)2.Node.js:v6.9.2LTS(64位) 二.安装Node.js步骤 1.下载对应你系统的Node.js版本:https:// ...

  4. 计蒜客 —— 字符串p型编码

    给定一个完全由数字字符('0','1','2',…,'9')构成的字符串 strstr,请写出 strstr 的 pp 型编码串. 例如:字符串122344111可被描述为“1个 1.2 个 2.1 ...

  5. Django:(03)请求和响应

    一.HttpRequest 客户端传参的几种方式 传递方式 示例 后端获取方式 数据类型 url路径(path) /news/1/2 正则匹配 str 查询字符串 /news2?category=1& ...

  6. python安装第三方库报错:Cannot uninstall '***'. It is a distutils installed project and thus we cannot accurately determine which files belong to it which would lead to only a partial uninstall.

    pip install --ignore-installed ${PACKAGE_NAME}

  7. Spring Cloud health节点通过注册中心扫描状态的简单实现

    package com.zjs.web; import com.netflix.appinfo.InstanceInfo; import com.zjs.FallbackApiApplication; ...

  8. 用maven建立一个工程

    建立java工程 mvn archetype:generate -DgroupId=cn.maxhou.demo -DartifactId=myapp -DarchetypeArtifactId=ma ...

  9. ubuntu下不能访问docker中的rabbitmq服务端口

    主要原因是防火墙屏蔽了15672端口,宿主机就不能直接通过 ip:port的形式访问rabbitmq的管理界面了. 解决方法很简单: 设置防火墙规则,使外部主机能够访问虚拟机的15672端口. 启动i ...

  10. python多进程单线程+协程实现高并发

    并发:看起来像同时运行就是并发 并行:同一时间同时被执行叫做并行,最大并行数就是CPU核数 协程不是实实在在存在的物理基础和操作系统运行逻辑,只是程序员从代码层面避开了系统对遇到IO的程序会切走CPU ...