0

Spring returns only the first param from the query string, subsequent params are missing.

When calling the following URL with curl:

 curl -i -X GET -b usercookie.txt -c usercookie.txt http://localhost:8080/appname/users/user-id/campaigns?title=JSON&itemPerPage=5&page=0&orderBy=startDate

Only the title param has non-null value, the request.getQueryString() contains only "title=JSON"

When calling this one:

curl -i -X GET -b usercookie.txt -c usercookie.txt http://localhost:8080/appname/users/user-id/campaigns?page=0&title=JSON&itemPerPage=5&orderBy=startDate

the request.getQueryString() contains only the "page=0"

Controller code:

@Controller
public class Campaign {

...

    @RequestMapping(value = {"/users/{userId}/campaigns", "/users/{userId}/campaigns/"}, 
            method = RequestMethod.GET)
    @ResponseBody
    public CampaignListResponse getCampaignList(
            @PathVariable(value="userId") String reqUserId,
            @RequestParam(required=false) Integer page, 
            @RequestParam(required=false) Integer itemPerPage,
            @RequestParam(required=false) String orderBy,
            @RequestParam(required=false) String status,
            @RequestParam(required=false) String title,                                         
            HttpServletRequest request,
            HttpServletResponse response,
            @CookieValue("session") String session) {
        LOGGER.debug("reqUserId:{} page:{}, itemPerPage:{}, orderBy:{}, state:{}, title:{}", reqUserId, page, itemPerPage, orderBy, status, title);
        LOGGER.debug("query string:{}", request.getQueryString());
...

What could be the cause of this? I expect all of the following params have a value both in the request.getQueryString(), and as individual @RequestParam variables:

page=0&title=JSON&itemPerPage=5&orderBy=startDate

Edit Spring Version: 3.2.2

bpgergo
  • 14,789
  • 2
  • 37
  • 60

2 Answers2

3

From your terminal just put the whole URL between double quotes ! & is a special character that asks the task to be run in background. You won't have to change your code.

Try this :

curl -i -X GET -b usercookie.txt -c usercookie.txt "http://localhost:8080/appname/users/user-id/campaigns?title=JSON&itemPerPage=5&page=0&orderBy=startDate"
Alexandre Jacob
  • 2,678
  • 1
  • 21
  • 30
0

Did you try to explicitly specify name of request parameters?

It works for me when I've specified them:

@RequestMapping({"/users/{userId}/campaigns", "/users/{userId}/campaigns/"})
@ResponseBody
public void getCampaignList(
    @PathVariable(value="userId") String reqUserId,
    @RequestParam(value = "page", required=false) Integer page, 
    @RequestParam(value = "itemPerPage", required=false) Integer itemPerPage,
    @RequestParam(value = "orderBy", required=false) String orderBy,
    @RequestParam(value = "status", required=false) String status,
    @RequestParam(value = "title", required=false) String title) {

Another option is to use JDK8 and enable -parameters javac option.

Slava Semushin
  • 13,753
  • 7
  • 47
  • 66