2015-06-09 20 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