首页 > 其他分享 >【swagger】@ApiImplicitParam注解Integer类型required=true时不能提交、@ApiImplicitParam注解dataType=“integer“不能传参问题

【swagger】@ApiImplicitParam注解Integer类型required=true时不能提交、@ApiImplicitParam注解dataType=“integer“不能传参问题

时间:2024-06-02 16:32:55浏览次数:25  
标签:传参 test2 ApiImplicitParam dataType required Integer 注解 true id

文章目录


以下内容基于swagger2.9.2进行讲解

<!--swagger2-->
<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>

一、问题描述

swagger中用@ApiImplicitParam注解的时候,若dataType=Integer类型且required=true时,在swagger页面中执行方法报红且不能提交。

当为其他类型如String时没有问题

@ApiOperation(value = "测试2")
@ApiImplicitParams(
        @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "Integer",paramType = "query")//dataType = "Integer",web页面无法进入请求
)
@GetMapping("/test2")
public void test2(Integer id){
    log.info("id"+id);
}

swagger页面报错如下:
当点击Execute后,报红不能提交
在这里插入图片描述

二、问题原因

猜测是因为dataType需要是私有类型,而不是包装类型。这个在1.5版本中有说明,不过没找到确切证据,有知道的也可以评论一下
https://docs.swagger.io/swagger-core/v1.5.X/apidocs/io/swagger/annotations/ApiImplicitParam.html#dataType()

三、解决方法

3.1、修改dataType=int

修改dataType=int且required=true时可提交通过

@ApiOperation(value = "测试2")
@ApiImplicitParams(
        @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "int",paramType = "query")
)
@GetMapping("/test2")
public void test2(Integer id){
    log.info("id"+id);
}

在这里插入图片描述

3.2、修改dataType=Long

修改dataType=Long,且required=true时可提交通过

@ApiOperation(value = "测试2")
@ApiImplicitParams(
        @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "Long",paramType = "query")
)
@GetMapping("/test2")
public void test2(Integer id){
    log.info("id"+id);
}

在这里插入图片描述

3.3、修改dataType类型为String类型

  1. 当dataType="String"类型时,required=true是没问题的
  2. 当dataType类型为Integer时,也可以直接删除dataType属性,这样默认就是String类型,此时required=true也是可以提交的
@ApiOperation(value = "测试2")
@ApiImplicitParams(
        @ApiImplicitParam(name = "id",value = "主键id",required = true,dataType = "String",paramType = "query")
)
@GetMapping("/test2")
public void test2(String id){
    log.info("id"+id);
}

在这里插入图片描述

3.4、当dataType类型为Integer时,删除required=true

当dataType类型为Integer时,若required不为true,也可提交通过

@ApiOperation(value = "测试2")
@ApiImplicitParams(
        @ApiImplicitParam(name = "id",value = "主键id"dataType = "Integer",paramType = "query")
)
@GetMapping("/test2")
public void test2(Integer id){
    log.info("id"+id);
}

在这里插入图片描述

标签:传参,test2,ApiImplicitParam,dataType,required,Integer,注解,true,id
From: https://blog.csdn.net/weixin_49114503/article/details/139391104

相关文章

  • 如何在Spring中使用@Query注解?
    在Spring中,@Query注解是一种非常强大的工具,它允许你在Repository接口中直接定义查询语句。使用@Query注解,你可以执行JPQL(JavaPersistenceQueryLanguage)查询或者原生SQL查询,从而实现复杂的数据库查询而无需编写自定义的数据访问代码。使用@Query注解的基......
  • @RequestMapping注解有哪些属性?
    在SpringFramework中,@RequestMapping注解用于将HTTP请求映射到MVC和REST控制器的处理方法上。它是SpringMVC中最基本的注解之一,可以应用在类级别或方法级别。@RequestMapping注解拥有多个属性,以下是其中的一些常用属性及其解释:value/path:类型:String[]描......
  • Java base(1):注解、泛型、通配符、重载、重写
    注解:用于在代码中插入元数据,不会直接影响程序的执行,但可以被编译器、开发工具或运行时环境用来处理特定任务,如编译时检查、生成额外的代码、进行框架级配置等。预定义注解:java给的,例如:@Override:用于标记一个方法是重写父类的方法。自定义注解元注解:注解其他注解的注解。元数......
  • SpringMVC:@RequestMapping注解
    1.@RequestMapping作用@RequestMapping`注解是SpringMVC框架中的一个控制器映射注解,用于将请求映射到相应的处理方法上。具体来说,它可以将指定URL的请求绑定到一个特定的方法或类上,从而实现对请求的处理和响应。2. 出现位置的区别出现在类上//@RequestMapping注......
  • 使用Spring Boot自定义注解 + AOP实现基于IP的接口限流和黑白名单
    ......
  • 零基础学Java第二十四天之注解的理解与使用
    注解1、什么是注解java.annotation包Annotation是从JDK1.5开始引入的新技术,注解即可以对程序员解释又可以对程序解释2、注解与注释的区别注释:对程序员解释代码信息注解:对程序和程序员解释代码信息3、注解的所用不是程序本身,可以对程序作出解释(与注释(comment)类......
  • Spring的@Async注解及其用途
    Spring的@Async注解是SpringFramework4.2版本引入的功能,它用于支持异步方法执行。当一个方法标注了@Async,Spring会在一个单独的线程中调用该方法,从而不会阻塞主线程的执行。@Async注解的用途:提高性能:通过异步执行,可以提高应用程序的响应性能,特别是在执行耗时的......
  • Shell阶段07 退出循环指令(示例:分发主机公钥), 函数应用(参数传参)
    退出循环的语句#1.exit退出循环,退出脚本#2.break结束当前循环,或者跳出本地循环,继续执行循环外面的命令#3.continue忽略本次循环剩余的代码,直接执行下一次循环#4.案例先扫描内网网段的所有主机,存活的主机进行发放本机的公钥1.本机是否要有公钥,创建密钥对rm2.......
  • springboot关键注解
    目录标记容器类注解1.@Controller 2.@Service3.@Repository4.@Component 依赖注入注解1.@Autowired2.@Resource@Autowired与@Resource的区别 web相关注解@RequestMapping属性介绍用法示例注意事项@GetMapping和@PostMapping @RestController......
  • java 注解和反射
    8.注解和反射8.1什么是注解annotationjkd5.0提供的8.2内置注解:@override覆盖方法@Deprecated不推荐使用,@suppressWarnings抑制警告信息:8.3元注解:解释其他注解的注解meta-annotation@Target描述注解范围@Retention注解生命周期:source<class<runtime@Documented:注......