我想通过参数传递我的日期,我不知道该怎么做。我尝试过EncodeUrl.encode(),但它没有用(有可能我做错了)
@Test
public void getUsageCountersParam2Test() throws Exception {
Date date = new Date(2017, 06, 23, 12, 39, 20);
MvcResult result = mockMvc.perform(get(getUri("/usages?apiConsumerId=[1,2]&serviceId=1&dateFrom=" + date)).contentType(MediaType.APPLICATION_JSON)).andExpect(status().isOk()).andReturn();
ObjectMapper mapper = new ObjectMapper();
List<UsageCounterDetailsDto> list = mapper.readValue(result.getResponse().getContentAsString(), new TypeReference<List<UsageCounterDetailsDto>>() {
});
assertNotNull(list);
assertEquals(3, list.size());
}
我有一个错误,例如:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.util.Date'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam java.util.Date] for value 'Mon Jul 23 12:39:20 CEST 3917'; nested exception is java.lang.IllegalArgumentException
希望你能帮助我。
答案 0 :(得分:0)
尝试这样的事情:
NULL
答案 1 :(得分:0)
我遇到了同样的问题,并发现将@DateTimeFormat注释添加到我的控制器有帮助。
有用的链接: How to accept Date params in a GET request to Spring MVC Controller?
Spring MVC controller with date
https://codingexplained.com/coding/java/spring-framework/date-parameter-in-spring-mvc-controller
答案 2 :(得分:0)
您可以使用@InitBinder注释来允许控制器接受不同的日期格式。