How to pass key value in one params in Spring REST

Dolphin :

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?

Artem Ptushkin :

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"

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=155763&siteId=1