2

I'm new to spring and I'm trying to add a @RequestParam of type Map<String, String> to my controller like this:

@RequestMapping(method = RequestMethod.GET)
public Model search(@RequestParam(value = "searchTerm", required = false) final String searchTerm,
        @RequestParam(value = "filters", required = false) Map<String, String> filters,
        final Model model) {

And in the URL it looks like this:

localhost/search?searchTerm=factory&filters[name]=factory1&filters[name]=factory2

But every time, filters is null, no matter what I do.

Can this be done? Thank you very much for your time!

2

1 Answer 1

1

Using Map with @RequestParam for multiple params If the method parameter is Map or MultiValueMap then the map is populated with all query string names and values. Following will be mapped with /employees/234/messages?sendBy=mgr&date=20160210

@RequestMapping("{id}/messages")
public String handleEmployeeMessagesRequest (@PathVariable("id") String employeeId,
                                        @RequestParam Map<String, String> queryMap,
                                        Model model) {
    model.addAttribute("msg", "employee request by id and query map : "+
              employeeId+", "+queryMap.toString());
    return "my-page";
}

Where employeeId = "234" and queryMap = {sendBy=mgr, date=20160210}

Sign up to request clarification or add additional context in comments.

1 Comment

According to the java doc, this is only true if the Map Request Param doesn't have a name. I can't figure out how to get it to work though with a name, despite the javadoc implying that you can

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.