【发布时间】:2015-08-24 08:19:41
【问题描述】:
问题是我的 Spring Rest Controller Request 上作为参数传递的 DateTime 未正确初始化。
控制器:
@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
传递的日期时间未正确初始化,日期正常,但时间设置为当前时间而不是给定时间。
谢谢。
【问题讨论】:
标签: java spring-mvc jodatime