springboot3.X 無法解析parameter參數(shù)問題分析
本文參考轉(zhuǎn)載:https://oldmoon.top/post/191
簡介
使用最新版的Springboot 3.2.1(我使用3.2.0)搭建開發(fā)環(huán)境進(jìn)行開發(fā),調(diào)用接口時出現(xiàn)奇怪的錯。報(bào)錯主要信息如下:
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新版本導(dǎo)致的。為什么會出現(xiàn)這個問題呢?原來是Spring 6.1之后,官方加強(qiáng)了很多錯誤校驗(yàn)和報(bào)錯提示,本文這個錯也是其中之一。
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) {
...
}官方說明中一直強(qiáng)調(diào)@PathVariable的使用,并沒有提及@RequestParam,參考官方文檔@RequestParam會發(fā)現(xiàn)最后有一句話:
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的使用是可選的(例如,設(shè)置其屬性)。 默認(rèn)情況下,任何簡單值類型(由 BeanUtils#isSimpleProperty 確定)且未由任何其他參數(shù)解析器解析的參數(shù)都將被視為使用@RequestParam注解。
根據(jù)原文及翻譯,這自然讓我認(rèn)為,@RequestParam依然是可以省略的。
然而奇怪的是,當(dāng)Springboot 3.2.1使用Maven管理項(xiàng)目時,如果不使用spring-boot-starter-parent作為父工程,那么接口中必須顯式聲明@RequestParam("name"),缺了其中的name也會報(bào)錯。我清晰地記得我在舊版本的 Springboot 中經(jīng)常省略 @RequestParam(“name”) 這種寫法。
但如果不使用spring-boot-starter-parent作為父工程,好像@RequestParam變成了不可省略注解。大家搭建微服務(wù)和多模塊時候,通常不會使用spring-boot-starter-parent作為父工程吧?還是只有我不用?。。。 還是盡量不要嘗試新版本,會少踩很多坑
錯誤代碼
當(dāng)請求URL中有正常參數(shù)時,如:http://localhost:8080/user/hello?name=zhangsan,其中name為一個參數(shù),你的Controller代碼大概如下所示:
@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>解決
這種現(xiàn)象不知道是不是官方的BUG,但目前我發(fā)現(xiàn)幾種解決方案:
- 在參數(shù)上使用
@RequestParam("name"): - 使用
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>maven-compiler-plugin
網(wǎng)友提除解決方案:父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標(biāo)志編譯代碼,從而使參數(shù)名稱在運(yùn)行時可用。
到此這篇關(guān)于springboot3.X 無法解析parameter參數(shù)問題的文章就介紹到這了,更多相關(guān)springboot無法解析parameter參數(shù)內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
Java多線程之鎖學(xué)習(xí)(增強(qiáng)版)
這篇文章主要為大家詳細(xì)介紹了Java多線程中鎖的相關(guān)知識,文中的示例代碼講解詳細(xì),對我們了解線程有一定幫助,需要的可以參考一下2023-02-02
一文精通Spring Boot序列化與反序列化(告別數(shù)據(jù)混亂)
本篇文章主要介紹了序列化與反序列化的概念,以及在SpringBoot中的應(yīng)用,文章通過通俗易懂的語言和比喻,深入淺出地解釋了這兩個過程,并展示了SpringBoot如何通過SpringMVC和Jackson庫簡化數(shù)據(jù)格式轉(zhuǎn)換,感興趣的朋友跟隨小編一起看看吧2026-03-03
DoytoQuery中的關(guān)聯(lián)查詢方案示例詳解
這篇文章主要為大家介紹了DoytoQuery中的關(guān)聯(lián)查詢方案示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-12-12
Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程
這篇文章主要介紹了Java實(shí)現(xiàn)Linux下雙守護(hù)進(jìn)程的思路、原理以及具體實(shí)現(xiàn)方式,非常的詳細(xì),希望對大家有所幫助2014-10-10
從 Spring Boot 3+Java 21 到 Spri
本文介紹了從SpringBoot3+Java21升級到SpringBoot4+Java25的具體步驟與注意事項(xiàng),主要包含環(huán)境與依賴自查、代碼適配、進(jìn)階優(yōu)化、測試與驗(yàn)證以及遷移checklist等內(nèi)容,感興趣的朋友跟隨小編一起看看吧2026-04-04
mybatis-generator 修改表結(jié)構(gòu)后實(shí)體不更新問題及解決方法
本文主要介紹了如何在MyBatis Generator中修改表結(jié)構(gòu)后更新實(shí)體類的問題,并提供了解決方案,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價值,需要的朋友參考下吧2025-04-04
Java查搜索文件內(nèi)容實(shí)現(xiàn)方式
用戶為解決無法搜索文件內(nèi)容的問題,編寫了一個支持單文件、文件夾及多層遞歸查找的工具,具備字符串匹配及忽略大小寫功能,并計(jì)劃擴(kuò)展日期、創(chuàng)建人等組合搜索條件,最終打包成exe便于快速查找所有文件內(nèi)容2025-09-09
JAVA基礎(chǔ)之?dāng)?shù)組和集合區(qū)別對比分析
文章主要介紹了Java中數(shù)組和集合的基本概念、使用方法以及它們之間的區(qū)別,文章還探討了不可變集合的創(chuàng)建方式及其線程安全和不可篡改的優(yōu)勢,感興趣的朋友跟隨小編一起看看吧2025-11-11

