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

這里使用@PathVariable注解來注解參數(shù),value中使用{參數(shù)名}來實現(xiàn)。
使用@GetMapping注解,用一個對象來接受參數(shù)報錯400
controller
@GetMapping("/products")
public ApiResult<List<YxStoreProductQueryVo>> goodsList(YxStoreProductQueryParam productQueryParam){
? ? ? ? return ApiResult.ok(storeProductService.getGoodsList(productQueryParam));
? ? }接受參數(shù)的實體
@Data
@EqualsAndHashCode(callSuper = true)
@ApiModel(value="YxStoreProductQueryParam對象", 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; ? ?
}請求地址:
http://127.0.0.1:8008/api/products?page=&limit=8&keyword=&sid=129&news=0&priceOrder=&salesOrder=
后臺打印報錯信息:
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類型的,要么就不傳參數(shù)要么就要傳遞一個具體的數(shù)
錯誤方法:
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=
以上為個人經(jīng)驗,希望能給大家一個參考,也希望大家多多支持腳本之家。
相關(guān)文章
Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法
本篇文章介紹了,Java 使用poi把數(shù)據(jù)庫中數(shù)據(jù)導(dǎo)入Excel的解決方法。需要的朋友參考下2013-05-05

