广告
返回顶部
首页 > 资讯 > 后端开发 > Python >基于@GetMapping注解携带参数的方式
  • 129
分享到

基于@GetMapping注解携带参数的方式

2024-04-02 19:04:59 129人浏览 安东尼

Python 官方文档:入门教程 => 点击学习

摘要

目录@GetMapping注解携带参数方式使用@GetMapping注解,用一个对象来接受参数报错400解决办法(总结)@GetMapping注解携带参数方式 今天突然发现,当我们根

@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= 

以上为个人经验,希望能给大家一个参考,也希望大家多多支持编程网。

--结束END--

本文标题: 基于@GetMapping注解携带参数的方式

本文链接: https://www.lsjlt.com/news/148089.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作