1

Spring 仅返回查询字符串中的第一个参数,缺少后续参数。

使用 curl 调用以下 URL 时:

 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

只有title参数有非空值,request.getQueryString()只包含“title=JSON”

调用这个时:

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

仅包含“ request.getQueryString()page=0”

控制器代码:

@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());
...

这可能是什么原因?我希望以下所有参数在 request.getQueryString() 和单个 @RequestParam 变量中都有一个值:

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

编辑春季版本:3.2.2

4

2 回答 2

4

从您的终端只需将整个 URL 放在双引号之间!&是一个特殊字符,要求任务在后台运行。您不必更改代码。

尝试这个 :

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"
于 2015-09-24T22:25:57.590 回答
0

您是否尝试明确指定请求参数的名称?

当我指定它们时,它对我有用:

@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) {

另一种选择是使用 JDK8 并启用-parameters javac选项。

于 2015-09-24T19:28:15.597 回答