2015-06-09 17 views
0

的问题是,日期时间过去了作为我的春节休息控制器请求参数不正确初始化。如何在获取初始化参数@DateTimeFormat?

控制器

@RequestMapping(value = "/events/{dateTimeStart}/{dateTimeEnd}", method = RequestMethod.GET, 
produces = MediaType.APPLICATION_JSON_VALUE) 
public ResponseEntity<List<Event>> getEventsWithinTime(@DateTimeFormat(iso=DateTimeFormat.ISO.DATE_TIME) DateTime dateTimeStart, @DateTimeFormat(iso=DateTimeFormat.ISO.DATE_TIME) DateTime dateTimeEnd) 
{ 
    log.debug("Rest Request: Getting Timestamp Events from " + dateTimeStart + " and " + dateTimeEnd); 
} 

测试:

@Test 
@Transactional 
public void checkEventWithingDateTime() throws Exception { 
    eventRepository.saveAndFlush(event); 

    LocalDate nowDateTime = new LocalDate(); 

    DateTime midnightDateTime = new DateTime(nowDateTime.getYear(), nowDateTime.getMonthOfYear(), 
     nowDateTime.getDayOfMonth(), 0,0); 

    DateTime lastSecondDateTime = new DateTime(nowDateTime.getYear(), nowDateTime.getMonthOfYear(), 
     nowDateTime.getDayOfMonth(), 23,59,59); 


    System.out.println("Event Start Date: " + event.getStartDate()); 
    System.out.println("Looking between : " + midnightDateTime.toDateTimeISO() + " and " + lastSecondDateTime.toDateTimeISO()); 

    restEventMockMvc.perform(get("/api/events/{timestampStart}/{timestampEnd}", midnightDateTime.toDateTimeISO(), lastSecondDateTime.toDateTimeISO())) 
     .andExpect(status().isOk()) 
     .andExpect(content().contentType(MediaType.APPLICATION_JSON)) 
     .andExpect(jsonPath("$").isArray()) 
     .andExpect(jsonPath("$", hasSize(1))) 
     ; 


} 

的问题:

Event Start Date: 2015-06-09T15:19:01.935+02:00 
Looking between : 2015-06-09T00:00:00.000+02:00 and 2015-06-09T23:59:59.000+02:00 
[DEBUG] com.ent.event.web.rest.EventResource - Rest Request: Getting Timestamp Events from 2015-06-09T15:19:02.681+02:00 and 2015-06-09T15:19:02.681+02:00 

日期时间PAS sed没有正确初始化,日期没问题,但时间设置为当前,而不是给定的。

谢谢。

回答

0

我记得我面临着加入DateTimeFormat的一些问题。最终我通过活页夹解决了情况:

@InitBinder 
public void initBinder(WebDataBinder binder) { 
    SimpleDateFormat dateFormat = new SimpleDateFormat("dd.MM.yyyy");; 
    dateFormat.setLenient(false); 
    binder.registerCustomEditor(Date.class, new CustomDateEditor(dateFormat, false)); 
} 
+0

这不适合我..感谢您的帮助。 – Sqrt