Spring MVC - How to Get All Request Params in a Map in Spring Controller

Mapping all request params into an object in Spring Controller

Since it is an API design requirement, it should be clearly reflected in the corresponding DTO's and endpoints.

Usually, this kind of requirement stems from a parallel change and implies that the old type queries will be disabled during the contract phase.

You could approach the requirement by adding the required mapping "query-parameter-name-to-property-name" by adding it to the ContactDTO. The simplest way would be just to add an additional setter like below

public class ContactDTO {
private Long id;
private String name;
private Long eventId;

public void setEvent_id(Long eventId) {
this.eventId = eventId;
}
}

If you prefer immutable DTO's, then providing a proper constructor should work as well

@Value
public class ContactDTO {
private Long id;
private String name;
private Long eventId;

public ContactDTO(Long id, String name, String eventId, String event_id) {
this.id = id;
this.name = name;
this.eventId = eventId != null ? eventId : event_id;
}
}

Java Spring how to get all request parameters

The answer is yes!
You can do this by injecting the HttpServletRequest object in your endpoint by simple doing:

@RequestMapping(value="/foo")
public String bar(HttpServletRequest request){
System.out.println("Here you can see the creation time: " + request.getSession().getCreationTime());
return "whatever string";
}

Then you can do just as following to list your parameters: Get servlet parameters

Improving it (suggestion from @BoristheSpider) you can use the MultiValueMap<String, String> object that already maps all of your parameters into one object. So you can do something like this:

@RequestMapping(value="/foo")
public String bar(@RequestParam MultiValueMap<String, String> parameters){ ... }

You can see an usage in here.

Spring Get all params from URL in Controller

I did it like this and worked, declaring months as array of Strings:

@RequestMapping(value = "/{year}/excel/", method = RequestMethod.GET, produces = EXCEL_FORMAT_HEADER)
public @ResponseBody
ModelAndView getRankingByYearExcel(@PathVariable("year") Integer year,
@RequestParam String[] months)

And in URL sent variable months as array of strings:

../2016/excel/?months=1,3,12

Thanks for guiding me in this direction

RequestParam with map is unnecessarily binding other request params in GET request

As mentioned in the comments, you can't exclude params for the @RequestParam map.

Do your client have the option to send those params with the body as a json object instead of as request parameters? A request body isn't normally sent in GET Requests, but you could do it. Then you would change @RequestParam<String, String> map) to @RequestBody Map<String, String> map)

[Spring MVC]- How can I retrieve all form params in a map?

Apply a @RequestParam annotation to the parameter. See more here : http://docs.spring.io/spring/docs/current/spring-framework-reference/html/mvc.html#mvc-ann-requestparam

"When an @RequestParam annotation is used on a Map or MultiValueMap argument, the map is populated with all request parameters."



Related Topics



Leave a reply



Submit