基于@GetMapping注解携带参数的方式
作者:程序逸 发布时间:2023-01-26 20:49:32
@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=
来源:https://blog.csdn.net/qq_43265564/article/details/117334172
猜你喜欢
- 前言去年在公司参与了一个某某机场建设智能机场的一个项目,人脸登机是其中的一个功能模块,当时只是写了后台的接口,调用人脸识别设备的api,给闸
- 前言: 之前安装了Ubuntu 18.04,结果在安装Codeblocks / VScode还是安装gcc,c/c++的时候出现了一堆错误(
- 1 背景去年有很多人私信告诉我让说说自定义控件,其实通观网络上的很多博客都在讲各种自定义控件,但是大多数都是授之以鱼,却很少有较
- Android平台有三种网络接口可以使用,他们分别是:java.net.*(标准Java接口)、Org.apache接口和Android.n
- 前言:本文主要介绍内容有:一个串行调用的例子(App首页信息查询)CompletionService实现并行调用抽取通用的并行调用方法代码思
- 前言各位小伙伴大家好,我是A哥。IDEA上个较大版本的发布,要追溯到4月份了:时隔近4个月,北京时间2020-07-28深夜,Intelli
- 一、返回一般数据类型比如要根据 id 属性获得数据库中的某个字段值。mapper 接口:// 根据 id 获得数据库中的 username
- 一.类与接口的区别类:描述了一个实体,包括实体的状态,也包括实体可能发出的动作。接口:定义了一个实体可能发出的动作。但是只是定义了这些动作的
- 程序生成的自定义文件,比如后缀是.test这种文件怎么直接启动打开程序,并打开本文件呢 1、
- Mybatis表现关联关系比hibernate简单,没有分那么细致one-to-many、many-to-one、one-to-one。而是
- 一、对象的创建1.1 new 类名虚拟机遇到一条new指令时,首先检查这个指令的参数是否能在常量池中定位到一个类的符号引用,并检查这个符号引
- 1.概述本文中,我将向你介绍Spring Cloud Netflix Turbine。它将多个Hystrix Metrics Streams
- 文件数据流在java语言中,进行文件输入和输出时,经常会使用到FileIntputStream和FileOutputStream两个文件数据
- Android调试出现The selected device is incompatible问题解决在做Android调试时碰到该问题。详情
- 前言有时候我们想克隆一个List去做别的事,而不影响原来的List,我们直接在list后面加上小点点,发现并没有Clone这样的扩展函数。这
- 前言我们了解数组这个概念之前,我们先思考下面几个问题。如果我们需要两个数据,那么直接创建两个变量即可int a;int b;如果需要五个数据
- 对谷歌地图操作使用的是WebBrowser控件,通过对javascript的操作来实现对谷歌地图的各种操作,所以首先要创建一个html文件,
- Java 利用poi把数据库中数据导入Excel效果:使用时先把poi包导入工程的path,注意只需要导入poi包即可,下载后有三个jar包
- 组合模式是一种常见的设计模式(但我感觉有点复杂)也叫合成模式,有时又叫做部分-整体模式,主要是用来描述部分与整体的关系。个人理解:组合模式就
- 一、代码先上代码(以下这段代码会有多种执行结果)@Testpublic void test_interrupted_thread() thr