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
Does a Tcp Socket Connection Have a "Keep Alive"
What Causes "Unable to Access Jarfile" Error
How to Split a Java String at Backslash
Execute Method on Startup in Spring
Converting Long to Date in Java Returns 1970
How to Get the Size of an Array, a Collection, or a String in Java
How Does Java "Week Year" Really Work
How to Sort a List by Different Parameters at Different Timed
Solve Hibernate Lazy-Init Issue with Hibernate.Enable_Lazy_Load_No_Trans
How to Implement a Db Listener in Java
When Will a String Be Garbage Collected in Java
How to Replace the Awt Eventqueue with Own Implementation
Break or Return from Java 8 Stream Foreach
Spring Aop Not Working for Method Call Inside Another Method
Create Java Console Inside a Gui Panel