In my javascript code, i have a date of format MM/DD/YYYY
. I need to pass this via request URL to spring controller.
I tried passing as String but because of /
cannot do it.
What would be the best way to send the date(MM/DD/YYYY)
to Spring controller.
js code:
var today = moment(); //it gives date as 05/26/2017
var requestURL = myContextPath + '/processDate/'+today+'/processDateForStack.form
controller:
@RequestMapping(value = "/{today}/processDateForStack", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public
@ResponseBody
String getprocessStartedDate(@PathVariable("today") String today) throws Exception{
System.out.println("date : " + today); //should display 05/26/2017
//logic here
}
PS: Here i have used date as String type, i can use as Date type also.
I tried sending the date from js as MM-DD-YYYY
from js code and in spring controller i have used @DateTimeFormat(pattern = "MM-dd-yyyy")
while declaring the arguments but was throwing exception. Any suggestions would be helpful.
--EDITED--
When i pass date value as String from javascript, value of today at spring controller is 1495823051245
.
When i pass date value as Date from javascript to spring controller, below is the exception generated.
Error: Failed to convert value of type 'java.lang.String' to required type 'java.sql.Date'; nested exception is org.springframework.core.convert.ConversionFailedException
When sent the date as Date from js to controller i have used @DateTimeFormat(pattern = "MM-dd-yyyy")
while declaring the pathvariable.