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?
While the other answers are correct it certainly is not the "Spring way" to use the HttpServletRequest object directly. The answer is actually quite simple and what you would expect if you're familiar with Spring MVC.
@RequestMapping(value = {"/search/", "/search"}, method = RequestMethod.GET)
public String search(
@RequestParam Map<String,String> allRequestParams, ModelMap model) {
return "viewName";
}
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.
@RequestParam
will gladly take a Map
as a parameter (see my answer).
@RequestParam
on a Map<String,String>
to retrieve all query string parameters. And please, don't feel so abhorred over the answers you see here...they aren't that bad :) static.springsource.org/spring/docs/3.1.x/javadoc-api/org/…
@RequestParam Map<>
way was not done. I have slight annoyance in that many modern projects I have seen Spring MVC (spring 3.1 and greater) they will put the HttpServletRequest and HttpServletResponse on every method. And it seems its because junior developers use StackOverflow and google instead of looking at the doc. This makes it difficult for the Spring project to switch from servlet api to say a Netty API. JAX-RS has similar issues of abuse but at a far lesser degree.
Here's a simple example of getting requestParams
in a Map
:
@RequestMapping(value="submitForm.html", method=RequestMethod.POST)
public ModelAndView submitForm(@RequestParam Map<String, String> reqParam) {
String name = reqParam.get("studentName");
String email = reqParam.get("studentEmail");
ModelAndView model = new ModelAndView("AdmissionSuccess");
model.addObject("msg", "Details submitted by you::Name: " + name
+ ", Email: " + email );
}
In this case, it will bind the values:
studentName with name
studentEmail with email
The HttpServletRequest object provides a map of parameters already. See request.getParameterMap() for more details.
you can simply use this:
Map<String, String[]> parameters = request.getParameterMap();
That should work fine
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.
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.:
@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.
String[]
as the value? I have to index it to 0 just to get the value.
key=val1,val2
or key=val1&key=val2
(if I remember correctly spring support both of these notations) so you will get array with 2 elements
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)
}
@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 be stored in jsonObject
.
There is fundamental difference between query parameters and path parameters. It goes like this: www.your_domain?queryparam1=1&queryparam2=2
- query parameters. www.your_domain/path_param1/entity/path_param2
- path parameters.
What I found surprising is that in Spring MVC world a lot of people confuse one for the other. While query parameters are more like criteria for a search, path params will most likely uniquely identify a resource. Having said that, it doesn't mean that you can't have multiple path parameters in your URI, because the resource structure can be nested. For example, let's say you need a specific car resource of a specific person:
www.my_site/customer/15/car/2
- looking for a second car of a 15th customer.
What would be a usecase to put all path parameters into a map? Path parameters don't have a "key" when you look at a URI itself, those keys inside the map would be taken from your @Mapping annotation, for example:
@GetMapping("/booking/{param1}/{param2}")
From HTTP/REST perspective path parameters can't be projected onto a map really. It's all about Spring's flexibility and their desire to accommodate any developers whim, in my opinion.
I would never use a map for path parameters, but it can be quite useful for query parameters.
Success story sharing
@RequestParam MultiValueMap<String, String>
import org.springframework.ui.ModelMap;
as well.ModelMap
isn't needed to get all request params as map; that's simply a detail specific to OP's code.