Convert Unix timestamp to Java Date, Spring RequestParam

The following is the fullcalendar js request to send to the server.

http://localhost:8080/NVB/rest/calendar/events?start=1425168000&end=1428796800 400 

How to specify a date pattern ( @DateTimeFormat ) in Spring Param request to convert this time to a Date object. I tried different templates, but got a 405 Bad Request.

 @RequestMapping(value = "/events", method = RequestMethod.GET) public @ResponseBody List<EventDto> addOrder(@RequestParam(value = "start") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date start, @RequestParam(value = "end") @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) Date end) { LOGGER.info("Requesting event from [{}] to [{}]", start, end); return new LinkedList<EventDto>(); } 
+5
source share
2 answers

Since timestamps are not a formatted date (following the Java SimpleDateFormat parameters), but more than a numeric value: I would recommend creating custom data binding for Date objects if you do this more often than this single example. See http://docs.spring.io/spring/docs/current/spring-framework-reference/htmlsingle/#portlet-ann-webdatabinder

As a one-time solution, you can bind them to Long parameters and create your own Date object using new Date(start) .

+1
source

Using @InitBinder and WebDataBinder :

 @RestController public class SimpleController { //... your handlers here... @InitBinder public void initBinder(final WebDataBinder webdataBinder) { webdataBinder.registerCustomEditor(Date.class, new PropertyEditorSupport() { @Override public void setAsText(String text) throws IllegalArgumentException { setValue(new Date(Long.valueOf(text))); } }); } } 
0
source

Source: https://habr.com/ru/post/1215872/


All Articles