Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing date to request param in Spring MVC

I am new to Spring MVC - and I am trying to pass a date from my javascript as a request Param

My controller looks something like -

public @ResponseBody List<RecordDisplay> getRecords(             @RequestParam(value="userID") Long userID,             @RequestParam(value="fromDate") Date fromDate,             @RequestParam(value="toDate") Date toDate) { 

The question I have is how do I make the call from javascript - as in what should the URL look like

for eg. - /getRecords?userID=1&fromDate=06022013&toDate=08022013' 

Do I need a way to parse the date so Spring can recognize it?

like image 212
user1755645 Avatar asked Feb 08 '13 06:02

user1755645


People also ask

How do you send a date in request parameter?

Convert Date Parameters on Request Level DATE) Date date) { // ... } @PostMapping("/local-date") public void localDate(@RequestParam("localDate") @DateTimeFormat(iso = DateTimeFormat. ISO. DATE) LocalDate localDate) { // ... }

How do I pass a date as query param in Postman?

set('currentdate', moment(). format(("YYYY-MM-DD"))); {{$timestamp}} -> predefined variable gets the current timestamp, Since you need date the above one should work. use the parameter {{currentdate}} in your GET request.

How do you pass a date as a parameter in Java?

Call the format() method of DateFormat class and pass the date object as a parameter to the method.

What is difference between @PathVariable and @RequestParam in spring?

Difference between @PathVariable and @RequestParam in Spring 1) The @RequestParam is used to extract query parameters while @PathVariable is used to extract data right from the URI.


1 Answers

Use @DateTimeFormat("MMddyyyy")

public @ResponseBody List<RecordDisplay> getRecords( @RequestParam(value="userID")  Long userID, @RequestParam(value="fromDate")     @DateTimeFormat(pattern="MMddyyyy") Date fromDate, @RequestParam(value="toDate")     @DateTimeFormat(pattern="MMddyyyy") Date toDate) { 
like image 150
Sudhakar Avatar answered Sep 21 '22 10:09

Sudhakar