Spring MVC - How to get all request params in a ma

2019-01-03 05:02发布

Sample URL:

../search/?attr1=value1&attr2=value2&attr4=value4

I do not know the names of attr1, att2, and attr4.

I would like to be able to do something like that (or similar, don't care, just as long as I have access to the Map of request param name -> value:

@RequestMapping(value = "/search/{parameters}", method = RequestMethod.GET)
public void search(HttpServletRequest request, 
@PathVariable Map<String,String> allRequestParams, ModelMap model)
throws Exception {//TODO: implement}

How can I achieve this with Spring MVC?

10条回答
Lonely孤独者°
2楼-- · 2019-01-03 05:30

I might be late to the party, but as per my understanding , you're looking for something like this :

for(String params : Collections.list(httpServletRequest.getParameterNames())) {
    // Whatever you want to do with your map
    // Key : params
    // Value : httpServletRequest.getParameter(params)                
}
查看更多
对你真心纯属浪费
3楼-- · 2019-01-03 05:31

Use org.springframework.web.context.request.WebRequest as a parameter in your controller method, it provides the method getParameterMap(), the advantage is that you do not tight your application to the Servlet API, the WebRequest is a example of JavaEE pattern Context Object.

查看更多
Fickle 薄情
4楼-- · 2019-01-03 05:33
        @SuppressWarnings("unchecked")
        Map<String,String[]> requestMapper=request.getParameterMap();
        JsonObject jsonObject=new JsonObject();
        for(String key:requestMapper.keySet()){
            jsonObject.addProperty(key, requestMapper.get(key)[0]);
        }

//all params will store into jsonObejct

查看更多
叛逆
5楼-- · 2019-01-03 05:37

The HttpServletRequest object provides a map of parameters already. See request.getParameterMap() for more details.

查看更多
The star\"
6楼-- · 2019-01-03 05:46

Edit

It has been pointed out that there exists (at least as of 3.0) a pure Spring MVC mechanism by which one could get this data. I will not detail it here, as it is the answer of another user. See @AdamGent's answer for details, and don't forget to upvote it.

In the Spring 3.2 documentation this mechanism is mentioned on both the RequestMapping JavaDoc page and the RequestParam JavaDoc page, but prior, it is only mentioned in the RequestMapping page. In 2.5 documentation there is no mention of this mechanism.

This is likely the preferred approach for most developers as it removes (at least this) binding to the HttpServletRequest object defined by the servlet-api jar.

/Edit

You should have access to the requests query string via request.getQueryString().

In addition to getQueryString, the query parameters can also be retrieved from request.getParameterMap() as a Map.

查看更多
女痞
7楼-- · 2019-01-03 05:50

There are two interfaces

  1. org.springframework.web.context.request.WebRequest
  2. org.springframework.web.context.request.NativeWebRequest

Allows for generic request parameter access as well as request/session attribute access, without ties to the native Servlet/Portlet API.

Ex.:

@RequestMapping(value = "/", method = GET)
public List<T> getAll(WebRequest webRequest){
    Map<String, String[]> params = webRequest.getParameterMap();
    //...
}

P.S. There are Docs about arguments which can be used as Controller params.

查看更多
登录 后发表回答