基于@GetMapping注解携带参数的方式(postmapping注解接收参数)

网友投稿 1056 2022-07-31


目录@GetMapping注解携带参数方式使用@GetMapping注解,用一个对象来接受参数报错400解决办法(总结)

@GetMapping注解携带参数方式

今天突然发现,当我们根据id查询用户信息时,如果不想通过localhost:8080//findOne?id=1来访问,而是通过localhost:8080//findOne/1这样的url来访问,结果找了一大圈都没有发现,现在来说明一下实现。

这里使用@PathVariable注解来注解参数,value中使用{参数名}来实现。

使用@GetMapping注解,用一个对象来接受参数报错400

controller

@GetMapping("/products")

public ApiResult> 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/ahttp://pi/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=


版权声明:本文内容由网络用户投稿,版权归原作者所有,本站不拥有其著作权,亦不承担相应法律责任。如果您发现本站中有涉嫌抄袭或描述失实的内容,请联系我们jiasou666@gmail.com 处理,核实后本网站将在24小时内删除侵权内容。

上一篇:Springboot PostMapping无法获取数据问题及解决(springboot项目实战)
下一篇:springboot中Getmapping获取参数的实现方式(springboot requestmapping原理)
相关文章

 发表评论

暂时没有评论,来抢沙发吧~