2020-07-01

Spring converting String to date or calendar in Pathparam / Queryparam / RequestParam

Spring converting String to date in Spring controller:

Send a date in YYYY-MM-DD format to a Spring Controller. Write a proper converter for that object, controller will able to hadle request.

Example of Converting string to date object with   Pathparam / Queryparam / RequestParam:

@RequestMapping(value="/fetch" , method=RequestMethod.GET)
 public @ResponseBody String fetchResult(@RequestParam("date") @DateTimeFormat(pattern="yyyy-MM-dd") Date date) {
        //Content goes here
}

@RequestMapping(value="/fetch/{date}" , method=RequestMethod.GET)
 public @ResponseBody String fetchResult(@PathParam("date") @DateTimeFormat(pattern="yyyy-MM-dd") Date date) {
        //Content goes here
}

No comments:

Post a Comment