基于@GetMapping注解攜帶參數(shù)的方式
@GetMapping注解攜帶參數(shù)方式
今天突然發(fā)現(xiàn),當(dāng)我們根據(jù)id查詢用戶信息時(shí),如果不想通過(guò)localhost:8080//findOne?id=1來(lái)訪問(wèn),而是通過(guò)localhost:8080//findOne/1這樣的url來(lái)訪問(wèn),結(jié)果找了一大圈都沒(méi)有發(fā)現(xiàn),現(xiàn)在來(lái)說(shuō)明一下實(shí)現(xiàn)。

這里使用@PathVariable注解來(lái)注解參數(shù),value中使用{參數(shù)名}來(lái)實(shí)現(xiàn)。
使用@GetMapping注解,用一個(gè)對(duì)象來(lái)接受參數(shù)報(bào)錯(cuò)400
controller
@GetMapping("/products")
public ApiResult<List<YxStoreProductQueryVo>> goodsList(YxStoreProductQueryParam productQueryParam){
? ? ? ? return ApiResult.ok(storeProductService.getGoodsList(productQueryParam));
? ? }接受參數(shù)的實(shí)體
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value="YxStoreProductQueryParam對(duì)象", description="商品表查詢參數(shù)")
public class YxStoreProductQueryParam extends QueryParam {
? ? private static final long serialVersionUID = 1L;
? ? private int page; ??
? ? private int limit;
? ? private int sid;
? ? private int cid;
? ? private int news;
? ? private String priceOrder;
? ? private String salesOrder;
? ? private String keyword;
? ? private Integer brandId; ? ?
}請(qǐng)求地址:
http://127.0.0.1:8008/api/products?page=&limit=8&keyword=&sid=129&news=0&priceOrder=&salesOrder=
后臺(tái)打印報(bào)錯(cuò)信息:
Field error in object 'yxStoreProductQueryParam' on field 'page': rejected value []; codes [typeMismatch.yxStoreProductQueryParam.page,typeMismatch.page,typeMismatch.int,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [yxStoreProductQueryParam.page,page]; arguments []; default message [ page ]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'int' for property 'page'; nested exception is java.lang.NumberFormatException: For input string: ""]
解決辦法(總結(jié))
如果參數(shù)是int或者Integer類(lèi)型的,要么就不傳參數(shù)要么就要傳遞一個(gè)具體的數(shù)
錯(cuò)誤方法:
http://127.0.0.1:8008/api/products?page=&limit=8&keyword=&sid=129&news=0&priceOrder=&salesOrder=
正確方法:
http://127.0.0.1:8008/api/products?limit=8&keyword=&sid=129&news=0&priceOrder=&salesOrder=
以上為個(gè)人經(jīng)驗(yàn),希望能給大家一個(gè)參考,也希望大家多多支持腳本之家。
相關(guān)文章
SpringBoot接口返回結(jié)果封裝方法實(shí)例詳解
在實(shí)際項(xiàng)目中,一般會(huì)把結(jié)果放在一個(gè)封裝類(lèi)中,封裝類(lèi)中包含http狀態(tài)值,狀態(tài)消息,以及實(shí)際的數(shù)據(jù)。這里主要記錄兩種方式,通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),需要的朋友參考下吧2021-09-09
MybatisPlus更新時(shí)部分失敗的問(wèn)題解決
這篇文章主要為大家詳細(xì)介紹了MybatisPlus更新時(shí)部分失敗的問(wèn)題分析和解決方法,文中的代碼示例講解的非常詳細(xì),需要的朋友可以參考下2023-06-06
Java 使用poi把數(shù)據(jù)庫(kù)中數(shù)據(jù)導(dǎo)入Excel的解決方法
本篇文章介紹了,Java 使用poi把數(shù)據(jù)庫(kù)中數(shù)據(jù)導(dǎo)入Excel的解決方法。需要的朋友參考下2013-05-05
springboot配置允許循環(huán)依賴問(wèn)題
這篇文章主要介紹了springboot配置允許循環(huán)依賴問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-05-05
10個(gè)Java解決內(nèi)存溢出OOM的方法詳解
在Java開(kāi)發(fā)過(guò)程中,有效的內(nèi)存管理是保證應(yīng)用程序穩(wěn)定性和性能的關(guān)鍵,不正確的內(nèi)存使用可能導(dǎo)致內(nèi)存泄露甚至是致命的OutOfMemoryError(OOM),下面我們就來(lái)學(xué)習(xí)一下有哪些解決辦法吧2024-01-01

