SpringBoot無法解析parameter參數(shù)問題的解決方法
簡介
使用最新版的 Springboot 3.2.1(我使用3.2.0)搭建開發(fā)環(huán)境進行開發(fā),調(diào)用接口時出現(xiàn)奇怪的錯。報錯主要信息如下:
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 之后,官方加強了很多錯誤校驗和報錯提示,本文這個錯也是其中之一。
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) {
...
}官方說明中一直強調(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è)置其屬性)。 默認情況下,任何簡單值類型(由 BeanUtils#isSimpleProperty 確定)且未由任何其他參數(shù)解析器解析的參數(shù)都將被視為使用@RequestParam注解。
根據(jù)原文及翻譯,這自然讓我認為,@RequestParam 依然是可以省略的。
然而奇怪的是,當(dāng) Springboot 3.2.1 使用Maven管理項目時,如果不使用 spring-boot-starter-parent 作為父工程,那么接口中必須顯式聲明 @RequestParam("name"),缺了其中的 name 也會報錯。我清晰地記得我在舊版本的 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>
<configuration>
<parameters>true</parameters>
</configuration>
</plugin>
</build> 這可確保使用 -parameters 標(biāo)志編譯代碼,從而使參數(shù)名稱在運行時可用。
到此這篇關(guān)于SpringBoot無法解析parameter參數(shù)問題的解決方法的文章就介紹到這了,更多相關(guān)SpringBoot無法解析parameter內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
使用springboot單元測試對weblistener的加載測試
這篇文章主要介紹了使用springboot單元測試對weblistener的加載測試,具有很好的參考價值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教2021-10-10
SpringBoot居然有44種應(yīng)用啟動器,你都知道嗎
很多人都不知道SpringBoot應(yīng)用啟動器竟然有44個,本文就一起來介紹一下,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧2021-01-01
Java如何將處理完異常之后的程序能夠從拋出異常的地點向下執(zhí)行?
今天小編就為大家分享一篇關(guān)于Java如何將處理完異常之后的程序能夠從拋出異常的地點向下執(zhí)行?,小編覺得內(nèi)容挺不錯的,現(xiàn)在分享給大家,具有很好的參考價值,需要的朋友一起跟隨小編來看看吧2019-04-04
在SpringBoot框架下實現(xiàn)Excel導(dǎo)入導(dǎo)出的方法詳解
SpringBoot是由Pivotal團隊提供的全新框架,其設(shè)計目的是用來簡化新Spring應(yīng)用的初始搭建以及開發(fā)過程,今天我們就使用純前對按表格控件帶大家了解,如何在Spring Boot框架下實現(xiàn)Excel服務(wù)端導(dǎo)入導(dǎo)出,需要的朋友可以參考下2023-06-06
???????Spring多租戶數(shù)據(jù)源管理 AbstractRoutingDataSource
本文技術(shù)了???????Spring多租戶數(shù)據(jù)源管理 AbstractRoutingDataSource,下文詳細內(nèi)容介紹,需要的小伙伴可以參考一下2022-05-05
Java服務(wù)調(diào)用失敗報Service com.oneinfinite.adflow.api.service.T
在Java開發(fā)中,服務(wù)調(diào)用是常見的操作,尤其是在微服務(wù)架構(gòu)中,然而,服務(wù)調(diào)用過程中可能會遇到各種問題,下面我們來看看如何解決Service com.oneinfinite.adflow.api.service.TestService with version 0.0.0 not found的問題吧2025-03-03

