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?
I might be late to the party, but as per my understanding , you're looking for something like this :
Use
org.springframework.web.context.request.WebRequest
as a parameter in your controller method, it provides the methodgetParameterMap()
, the advantage is that you do not tight your application to the Servlet API, the WebRequest is a example of JavaEE pattern Context Object.//all params will store into jsonObejct
The HttpServletRequest object provides a map of parameters already. See request.getParameterMap() for more details.
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 theRequestParam
JavaDoc page, but prior, it is only mentioned in theRequestMapping
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.
There are two interfaces
org.springframework.web.context.request.WebRequest
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.:
P.S. There are Docs about arguments which can be used as Controller params.