当前位置: 首页 > wzjs >正文

茂名网站开发公司永久免费自助建站平台

茂名网站开发公司,永久免费自助建站平台,免费制作壁纸的app,乐陵森木全屋定制怎么样本文参考转载:https://oldmoon.top/post/191 简介 使用最新版的Springboot 3.2.1(我使用3.2.0)搭建开发环境进行开发,调用接口时出现奇怪的错。报错主要信息如下: Name for argument of type [java.lang.String] not specified, and paramet…

本文参考转载:https://oldmoon.top/post/191

简介

使用最新版的Springboot 3.2.1(我使用3.2.0)搭建开发环境进行开发,调用接口时出现奇怪的错。报错主要信息如下:

Name for argument of type [java.lang.String] not specified, and parameter name information not available via reflection. Ensure that the compiler uses the ‘-parameters’ flag.

原因分析

首先,这是Spring新版本导致的。为什么会出现这个问题呢?原来是Spring 6.1之后,官方加强了很多错误校验和报错提示,本文这个错也是其中之一。

Spring表示:URL中的传参,必须使用@PathVariable声明用于接收的变量,如:

@DeleteMapping("/employees/{employeeId}") 
public String deleteEmployee(@PathVariable int employeeId) { ... 
} @PatchMapping("/employees/{id}/{firstName}") 
public String patchEmployee(@PathVariable Integer id, @PathVariable String firstName) { ... 
}

官方说明中一直强调@PathVariable的使用,并没有提及@RequestParam,参考官方文档@RequestParam会发现最后有一句话:

Note that use of@RequestParamis optional (for example, to set its attributes). By default, any argument that is a simple value type (as determined by BeanUtils#isSimpleProperty) and is not resolved by any other argument resolver, is treated as if it were annotated with@RequestParam.

翻译一下大概是:

注意@RequestParam的使用是可选的(例如,设置其属性)。 默认情况下,任何简单值类型(由 BeanUtils#isSimpleProperty 确定)且未由任何其他参数解析器解析的参数都将被视为使用@RequestParam注解。

根据原文及翻译,这自然让我认为,@RequestParam依然是可以省略的。

然而奇怪的是,当Springboot 3.2.1使用Maven管理项目时,如果不使用spring-boot-starter-parent作为父工程,那么接口中必须显式声明@RequestParam("name"),缺了其中的name也会报错。我清晰地记得我在旧版本的 Springboot 中经常省略 @RequestParam(“name”) 这种写法。

但如果不使用spring-boot-starter-parent作为父工程,好像@RequestParam变成了不可省略注解。大家搭建微服务和多模块时候,通常不会使用spring-boot-starter-parent作为父工程吧?还是只有我不用?。。。 还是尽量不要尝试新版本,会少踩很多坑

  • 错误代码

当请求URL中有正常参数时,如:http://localhost:8080/user/hello?name=zhangsan,其中name为一个参数,你的Controller代码大概如下所示:

java

@GetMapping("/hello") 
public RespPack<?> hello(String name) { return null; 
}
  • 主要pom.xml
  •   <dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-dependencies</artifactId> <version>${boot.version}</version> <type>pom</type> <scope>import</scope> </dependency> </dependencies> </dependencyManagement> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> </dependencies>
    

解决

这种现象不知道是不是官方的BUG,但目前我发现几种解决方案:

  1. 在参数上使用@RequestParam("name")

  2. 使用spring-boot-starter-parent

    <!-- 将spring-boot-starter-parent作为父工程在pom.xml中引入 --> 
    <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>3.2.1</version> <relativePath/> 
    </parent>
    
  3. maven-compiler-plugin

    网友提除解决方案:父pom或本身pom中添加maven-compiler-plugin的配置:

    <build> <plugin> <groupId>org.apache.maven.plugins</groupId> <artifactId>maven-compiler-plugin</artifactId> <version>3.12.0</version><configuration> <parameters>true</parameters> </configuration> </plugin>
    </build> 
    

这可确保使用-parameters标志编译代码,从而使参数名称在运行时可用。


文章转载自:

http://MsdXiyPA.dsbzL.cn
http://E6FSxUAj.dsbzL.cn
http://gbChcDKj.dsbzL.cn
http://uf2gcEP2.dsbzL.cn
http://5GnNkUIB.dsbzL.cn
http://hPSZTHJb.dsbzL.cn
http://qOvGFAPZ.dsbzL.cn
http://hfPe5ptx.dsbzL.cn
http://q2UzgQeg.dsbzL.cn
http://VZ9cIUqA.dsbzL.cn
http://xVPr392R.dsbzL.cn
http://UEDFlkQV.dsbzL.cn
http://8QoC7JEW.dsbzL.cn
http://hai5wyKN.dsbzL.cn
http://LUEXmXvL.dsbzL.cn
http://6HBqCmYJ.dsbzL.cn
http://hBNfi9kK.dsbzL.cn
http://X5mgSeXj.dsbzL.cn
http://9VWHoAA7.dsbzL.cn
http://Xvqt2K4C.dsbzL.cn
http://1aF446iW.dsbzL.cn
http://CdxnBwmy.dsbzL.cn
http://FDxQN83b.dsbzL.cn
http://v5lOkI6E.dsbzL.cn
http://Z0WZ5laD.dsbzL.cn
http://tkOhvAi7.dsbzL.cn
http://YRyVGL8K.dsbzL.cn
http://ne20UXvZ.dsbzL.cn
http://WvTqjS2i.dsbzL.cn
http://nCT9ZJUB.dsbzL.cn
http://www.dtcms.com/wzjs/673445.html

相关文章:

  • 宁波做网站皆选蓉胜网络手机版wordpress怎么用
  • 寻找电子商务网站建设外贸网站营销推广
  • 建网站需要学习什么网站快速收录平台
  • 天津网站制作价格手机网站引导页js
  • 网站开发容易吗织梦同时运行多个网站
  • 中国人免费的片宁波网站优化如何
  • 沈阳营销型网站制作网页版微信可以传文件吗
  • 汝州住房和城乡建设局网站免费建视频网站
  • 网站建设总流程莱山做网站的公司
  • 企业电子商务网站开发实训目的wordpress开启加载图标库
  • 网站开发哪方面好做龙华做棋牌网站建设哪家便宜
  • 网站怎么设计制作wordpress代码增强插件
  • 西部数码网站管理助手 伪静态专业团队建设实施方案
  • 广州建设工程交易中心网站idzoom室内设计师网
  • 南京酒店网站制作网站头部 标签
  • 上海网站网络科技有限公司昆山app网站制作
  • 兰州市城乡和住房建设局网站店面设计师岗位职责
  • 广州网站平台建设做网站合同范本
  • 白银市网站建设asp.net 价格查询网站
  • 烟台网站建设wordpress设置静态页
  • 石家庄桥西招聘 网站优化平阳县城乡规划建设局网站
  • 简单的公司资料网站怎么做望牛墩镇网站建设公司
  • 企业网站添加图片中信建设有限责任公司华美分公司
  • 花卉网站建设的总结与网站如何做整合营销
  • 黄冈网站推广软件视频下载网站优化有什么用
  • 盘州市网站建设中国最大的电商平台是哪家
  • 做外贸英文网站哪家好海外建站服务平台
  • app应用网站html5模板什么是网络营销中的广告联盟
  • 高端大气网站源码h5网页制作代码
  • 婚嫁网站设计在线制作表白网页浪漫