@getmapping注解携带参数方式
今天突然发现,当我们根据id查询用户信息时,如果不想通过localhost:8080//findone?id=1来访问,而是通过localhost:8080//findone/1这样的url来访问,结果找了一大圈都没有发现,现在来说明一下实现。
这里使用@pathvariable注解来注解参数,value中使用{参数名}来实现。
使用@getmapping注解,用一个对象来接受参数报错400
controller
@getmapping("/products") public apiresult<list<yxstoreproductqueryvo>> goodslist(yxstoreproductqueryparam productqueryparam){ return apiresult.ok(storeproductservice.getgoodslist(productqueryparam)); }
接受参数的实体
@data @equalsandhashcode(callsuper = true) @apimodel(value="yxstoreproductqueryparam对象", description="商品表查询参数") 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: ""]
解决办法(总结)
如果参数是int或者integer类型的,要么就不传参数要么就要传递一个具体的数
错误方法:
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=
以上为个人经验,希望能给大家一个参考,也希望大家多多支持。