问题现象:

今天在学习swagger做接口api说明的时候,出现了一个一直解决不了的问题,而且网上搜了很久,都找不到任何相似的问题和解决方法:

当用swagger测试一个需要传入(Integer数据类型)参数的接口时,一直是显示红框状态,不能被execute(执行),没有任何错误提示!


问题分析:

于是我就通过以下几个方面去查看问题所在:

1.swagger依赖:(没问题)

        <!--swaggerUI--><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger2</artifactId><version>2.9.2</version></dependency><dependency><groupId>io.springfox</groupId><artifactId>springfox-swagger-ui</artifactId><version>2.9.2</version></dependency>

2.swagger配置类(没问题):

import org.springframework.beans.factory.annotation.Value;
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.service.Contact;
import springfox.documentation.spi.DocumentationType;
import springfox.documentation.spring.web.plugins.Docket;
import springfox.documentation.swagger2.annotations.EnableSwagger2;@Configuration
@EnableSwagger2//注解开启 swagger2 功能
public class Swagger2Config {//是否开启swagger,正式环境一般是需要关闭的@Value("${swagger.enabled}")private boolean enableSwagger;@Beanpublic Docket createRestApi() {return new Docket(DocumentationType.SWAGGER_2).apiInfo(apiInfo()) //信息会在页面上展示.enable(enableSwagger)//是否开启 (true 开启  false隐藏。生产环境建议隐藏).select().apis(RequestHandlerSelectors.basePackage("com.stephen.shopproduct.controller"))//扫描的路径包,设置basePackage会将包下的所有被@Api标记类的所有方法作为api.paths(PathSelectors.any())//指定路径处理PathSelectors.any()代表所有的路径.build();}private ApiInfo apiInfo() {return new ApiInfoBuilder().title("Product使用Swagger2构建RESTful接口")//设置文档标题(API名称).description("接口说明")//文档描述.termsOfServiceUrl("http://127.0.0.1:8081/")//服务条款URL// 注意URL要和配置文件中的端口号一致,否则会访问不了http://127.0.0.1:8091/swagger-ui.html.contact(new Contact("stephen", "http://127.0.0.1:8081/", "thpower@sgy.com"))//联系信息.version("1.0.0")//版本号.description("API 内容描述").build();}
}

3.Controller控制层

import com.alibaba.fastjson.JSON;
import com.stephen.shopproduct.service.ProductService;
import com.stephen.shopcommon.model.Product;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;@RestController
@Slf4j
@Api(value = "商品接口功能", tags = "ProductController", description = "商品接口相关介绍")
public class ProductController {@Autowiredprivate ProductService productService;@ApiOperation(value = "根据商品pid查询商品信息", notes = "商品信息pid")@ApiImplicitParam(name = "pid", value = "商品id", required = true, dataType = "Integer", paramType = "path")@GetMapping("/product/{pid}")public Product product(@PathVariable("pid") Integer pid) {Product product = productService.findByPid(pid);log.info("查询到商品:" + JSON.toJSONString(product));return product;}
}

仔细检查发现都没有问题啊!我一开始猜测可能是swagger版本的问题,于是我试着再建一个接口先测试一下:

    @ApiOperation(value = "根据商品id查询商品信息", notes = "商品信息id")@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "String", paramType = "path")@GetMapping("/prod/{id}")public Product product(@PathVariable("id") String id) {Product product = new Product();product.setPid(3);product.setPname("好东西");product.setPprice(10.0);product.setStock(1000);return product;}

接口测试成功!这说明依赖包和配置类都没有问题,那么就只能是这个控制层出问题了,我想到两个方向:

1.接口上的swagger注解属性配置有问题.

2.swaggerUI页面传入参数的格式有问题.

通过网上查询的资料我发现:测试接口传入参数的时候基本上都是直接输入值即可,因此排除了 "2.swaggerUI页面传入参数的格式有问题." 这个原因.

那就只剩下第一个原因了:swagger注解属性配置有问题; 于是我又看了一遍接口的注解配置: @ApiOperation@ApiImplicitParam

    @ApiOperation(value = "根据商品pid查询商品信息", notes = "商品信息pid")@ApiImplicitParam(name = "pid", value = "商品id", required = true, dataType = "Integer", paramType = "path")@GetMapping("/product/{pid}")public Product product(@PathVariable("pid") Integer pid) {Product product = productService.findByPid(pid);log.info("查询到商品:" + JSON.toJSONString(product));return product;}@ApiOperation(value = "根据商品id查询商品信息", notes = "商品信息id")@ApiImplicitParam(name = "id", value = "商品id", required = true, dataType = "String", paramType = "path")@GetMapping("/prod/{id}")public Product product(@PathVariable("id") String id) {Product product = new Product();product.setPid(3);product.setPname("好东西");product.setPprice(10.0);product.setStock(1000);return product;}

看了半天也没看出有什么问题:上面的接口测试时传入参数失败,而下面的接口却能成功!

这两个接口唯一的有效区别就是参数的类型了!但是查看了一下两个接口的dataType和接口参数的数据类型是一样的!

尽管我知道一定是参数配置出了问题,但就是不知道问题在哪里!就在我百思不得其解,网上又找不到答案的情况下,

我想着如果不指定参数类型会怎么样呢?于是把dataType属性去掉,发现成功了:

但是有一个细节,可能很多人没有注意到,接口的参数类型显示是错的(并不是Integer):

这里显示的是string类型,这说明当不指定dataType的时候,默认是传入String类型的参数,

但不知道为什么会自动检测并匹配了接口参数类型?得看看源码才知道了!

这个问题要是有知情的小伙伴请一定在评论区留下你的精彩评论,感谢.

后来突发奇想: 如果参数类型是Integer,那对应的dataType有没有可能是int?

结果真的如我所料,修改之后接口测试成功了(神奇):

而且接口的参数类型显示是对的(integer):

虽然解决了问题,但我还是建议swagger官方可以解决这个bug,让dataType和参数类型对应起来,Integer就应该对应Integer,int对应int,毕竟学过java的都知道这两者不是同一类型的,不然很容易混淆!!!!!!


解决方法:

    @ApiOperation(value = "根据商品pid查询商品信息", notes = "商品信息pid")@ApiImplicitParam(name = "pid", value = "商品id", required = true, dataType = "Integer", paramType = "path")@GetMapping("/product/{pid}")public Product product(@PathVariable("pid") Integer pid) {Product product = productService.findByPid(pid);log.info("查询到商品:" + JSON.toJSONString(product));return product;}

@ApiImplicitParam 中 dataType 的值 Integer 修改为 int:

    @ApiOperation(value = "根据商品pid查询商品信息", notes = "商品信息pid")@ApiImplicitParam(name = "pid", value = "商品id", required = true, dataType = "int", paramType = "path")@GetMapping("/product/{pid}")public Product product(@PathVariable("pid") Integer pid) {Product product = productService.findByPid(pid);log.info("查询到商品:" + JSON.toJSONString(product));return product;}

swagger学习日记1 swagger测试接口时传入参数的类型问题相关推荐

  1. 使用Postman测试接口时绕过登录

    开发中遇到的坑之测试 使用Postman测试接口时绕过登录 使用Postman测试接口时绕过登录 1.使用Postman测试接口时需要登陆验证,PostMan无法正常完成测试. 2.首先使用浏览器登录 ...

  2. [python教程入门学习]python学习笔记(CMD执行文件并传入参数)

    本文章向大家介绍python学习笔记(CMD执行文件并传入参数),主要包括python学习笔记(CMD执行文件并传入参数)使用实例.应用技巧.基本知识点总结和需要注意事项,具有一定的参考价值,需要的朋 ...

  3. 使用ApiPost测试接口时需要先登录的接口怎么办(基于Cookie)?

    在后台在开发.调试接口时,常常会遇到需要登陆才能请求的接口. 比如:获取登陆用户的收藏列表,此时,我们就需要模拟登陆状态进行接口调试了.如图: 今天,我们讲解利用ApiPost的环境变量,解决这种需要 ...

  4. mockito+junit 单元测试 测试私有方法利用反射去调用提升覆盖率 反射调用时传入参数为 null

    今天公司安排我写单元测试 因为要通过三级认证 公司要求是覆盖率必须达到100% 而在写单元测试的时候 对于是否要测试私有方法一直是一个争议 公司规范 对私有方法也进行测试 代码如下: private ...

  5. swagger学习日记4 文档无法识别下划线属性名

    目录 问题分析: 问题现象: 3.回到最开始的问题:如何在让swagger文档识别带下划线(_)的属性名? 解决方法: 问题分析: 今天在使用swagger注解接口,编写文档的时候,发现了一个大问题: ...

  6. Android测试接口时超时

    android设备和测试服务器处于同一局域网内测试时,出现该问题. 报错如下: java.net.ConnectException: failed to connect to /192.168.1.1 ...

  7. 使用Postman测试接口时记录token方便使用

    场景: 写后台api接口,每次使用postman测试时,需要校验用户token,每次手动去登录复制非常麻烦. 解决办法: postman自带"Tests"模块支持请求结束之后执行脚 ...

  8. 我的职业生涯规划学习日记(软件工程)整理时间线

    学习路线 语言只是工具不过学c++一直都做不出来东西是为什么呢,下图要第六部才做项目 概述 大一上学期:C,C++,数据结构,c++stl源码,python爬虫进入物联网实验室python方向,进入算 ...

  9. 测试接口时使用PostMan怎么设置全局变量?

    TSINGSEE青犀视频凭借多年在视频研发领域的技术积累,在视频监控领域已经拥有丰富的项目实践经验,为用户解决了大量关于视频领域的痛点和难题,在我们通过TSINGSEE青犀平台调用postman时,会 ...

最新文章

  1. 经典算法学习——直接选择排序
  2. opencv+VS如何运行
  3. 山东计算机基础模拟题及答案,2016山东农信社考试模拟题--计算机基础知识答案(1)...
  4. 使用Fresco加载图片
  5. win11 通过winget安装/卸载v2raya的命令整理
  6. 2021年互联网公司“死亡”名单!(附清单)
  7. No compiler is provided in this environment. Perhaps you are running on a JRE rather than a JDK
  8. FloatingActionMenu 向上弹出菜单
  9. mysql 及时点还原_mysqlbinglog基于即时点还原
  10. Codeforces 776D The Door Problem
  11. java判断一个月间隔_如何检查间隔列表(Joda-Time)是否完全涵盖Java中的一个月
  12. System.Diagnostics.Process的常见用法
  13. php城市 省份,怎么获取省份和城市?
  14. C++使用OLE高速读写EXCEL的源码
  15. XZ_iOS 之开发者账号续费 你的支付授权失败 请核对你的信息并重试 或尝试其他支付方式 请联系你的银行了解更多信息
  16. Linux上安装软件软件汇总
  17. Unity相机漫游脚本
  18. 02-走马灯 动画实现图片无缝展示
  19. GPS之MTK平台代码小结以及gps协议注释
  20. 开源在线excel编辑器_推荐3款爽到爆的在线网站

热门文章

  1. KeyError: ‘layer1.1.bn1.bias‘
  2. 《10分钟学会结构化思考》一张掌握系统思维方法的思维导图
  3. Cocos2dx游戏源码合集(BY懒骨头+持续更新+2014.03.17)
  4. 提供Oracle账号仅用于jdk 下载
  5. x1 carbon 扩展屏 模糊
  6. c语言单链表(创建,插入,遍历,打印)
  7. 说一说那些奇葩的面试经历
  8. 【citavi使用】使用BibTex方式导入文献
  9. 对于跨境电商ERP系统的介绍
  10. PS基础操作之人像处理