Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to pass key value in one params in Spring REST

Tags:

java

spring

I want to pass key value pair in Spring REST params using this command:

curl http://localhost:11004/room/page?pageNum=1\&pageSize=1\&params={"key":"value"} -H "token:1"|jq '.'

but the server side received params parameter by string like this:

params="key:value"

How to make the server side get key value pair?This is the server side model code:

public class PageQuery<T> {
    private Integer pageNum;
    private Integer pageSize;

    private T params;
}

This is the server side function:

public Response<PageBean<Room>> queryRoomByPage(PageQuery<Map<String,String>> request);

How should I tweak my request or server side code to make it possible?

like image 521
Dolphin Avatar asked Dec 07 '25 10:12

Dolphin


1 Answers

Here pageNum=1\&pageSize=1\&params={"key":"value"} every part splits by & then it leads to your params="key:value" because here Map<String,String> you said to Spring that there is should be string at key and value

Example to change method input:

@RequestParam("pageNum") Integer pageNum, @RequestParam("pageSize") Integer pageSize, @RequestParam("params") Map<String, String>

Do not use such a complicated HTTP query parameter as json, please use http body for this and await specific input JSON for which you have a specific description represented by java class

Better: do not use String, if your input can be represented as a specific business object, especially at Map. Neither HTTP client nor other engineers won't understand it, at least they "just know"

like image 155
Artem Ptushkin Avatar answered Dec 10 '25 00:12

Artem Ptushkin