springboot3.X 無(wú)法解析parameter參數(shù)問(wèn)題分析
本文參考轉(zhuǎn)載:https://oldmoon.top/post/191
簡(jiǎn)介
使用最新版的Springboot 3.2.1(我使用3.2.0)搭建開(kāi)發(fā)環(huán)境進(jìn)行開(kāi)發(fā),調(diào)用接口時(shí)出現(xiàn)奇怪的錯(cuò)。報(bào)錯(cuò)主要信息如下:
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)致的。為什么會(huì)出現(xiàn)這個(gè)問(wèn)題呢?原來(lái)是Spring 6.1之后,官方加強(qiáng)了很多錯(cuò)誤校驗(yàn)和報(bào)錯(cuò)提示,本文這個(gè)錯(cuò)也是其中之一。
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) {
...
}官方說(shuō)明中一直強(qiáng)調(diào)@PathVariable的使用,并沒(méi)有提及@RequestParam,參考官方文檔@RequestParam會(huì)發(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)情況下,任何簡(jiǎn)單值類型(由 BeanUtils#isSimpleProperty 確定)且未由任何其他參數(shù)解析器解析的參數(shù)都將被視為使用@RequestParam注解。
根據(jù)原文及翻譯,這自然讓我認(rèn)為,@RequestParam依然是可以省略的。
然而奇怪的是,當(dāng)Springboot 3.2.1使用Maven管理項(xiàng)目時(shí),如果不使用spring-boot-starter-parent作為父工程,那么接口中必須顯式聲明@RequestParam("name"),缺了其中的name也會(huì)報(bào)錯(cuò)。我清晰地記得我在舊版本的 Springboot 中經(jīng)常省略 @RequestParam(“name”) 這種寫(xiě)法。
但如果不使用spring-boot-starter-parent作為父工程,好像@RequestParam變成了不可省略注解。大家搭建微服務(wù)和多模塊時(shí)候,通常不會(huì)使用spring-boot-starter-parent作為父工程吧?還是只有我不用?。。。 還是盡量不要嘗試新版本,會(huì)少踩很多坑
錯(cuò)誤代碼
當(dāng)請(qǐng)求URL中有正常參數(shù)時(shí),如:http://localhost:8080/user/hello?name=zhangsan,其中name為一個(gè)參數(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)行時(shí)可用。
到此這篇關(guān)于springboot3.X 無(wú)法解析parameter參數(shù)問(wèn)題的文章就介紹到這了,更多相關(guān)springboot無(wú)法解析parameter參數(shù)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
java編程創(chuàng)建型設(shè)計(jì)模式工廠方法模式示例詳解
這篇文章主要為大家介紹了java編程創(chuàng)建型設(shè)計(jì)模式之工廠方法模式的創(chuàng)建及案例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助2022-02-02
java中常見(jiàn)XML解析器的使用詳解(JAXP,DOM4J,Jsoup,JsoupXPath)
為了處理和操作XML數(shù)據(jù),我們需要使用XML解析器,本文將介紹幾種常用的XML解析器,包括JAXP、DOM4J、Jsoup和JsoupXPath,需要的小伙伴可以參考一下2023-11-11
Java使用jdbc連接MySQL數(shù)據(jù)庫(kù)實(shí)例分析
這篇文章主要介紹了Java使用jdbc連接MySQL數(shù)據(jù)庫(kù),結(jié)合實(shí)例形式分析了Java基于jdbc鏈接mysql的相關(guān)配置及工具類的定義相關(guān)操作技巧,需要的朋友可以參考下2018-07-07
Spring Data JPA中 in 條件參數(shù)的傳遞方式
這篇文章主要介紹了Spring Data JPA中 in 條件參數(shù)的傳遞方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2021-06-06
httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法
下面小編就為大家?guī)?lái)一篇httpclient模擬post請(qǐng)求json封裝表單數(shù)據(jù)的實(shí)現(xiàn)方法。小編覺(jué)得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過(guò)來(lái)看看吧2016-12-12
Springboot項(xiàng)目因?yàn)閗ackson版本問(wèn)題啟動(dòng)報(bào)錯(cuò)解決方案
這篇文章主要介紹了Springboot項(xiàng)目因?yàn)閗ackson版本問(wèn)題啟動(dòng)報(bào)錯(cuò)解決方案,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-07-07
Mybatis的類型轉(zhuǎn)換接口TypeHandler
這篇文章主要介紹了Mybatis的類型轉(zhuǎn)換接口TypeHandler,文章圍繞主題展開(kāi)詳細(xì)的內(nèi)容介紹,具有一定的參考價(jià)值,需要的朋友可以參考一下2022-08-08
springboot日期格式化及時(shí)差問(wèn)題分析
這篇文章主要介紹了springboot日期格式化,時(shí)差問(wèn)題,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-12-12

