使用 LocalDate 类型参数发送请求
Sending requests with LocalDate type parameters
我想发送这样的请求:
- http://localhost:8080/reports/daily?start_date=2018-03-22&end_date=2018-03-24
我有一个错误:
"2018-05-31 15:40:29.623 WARN 11496 --- [nio-8080-exec-5]
.w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request
element:
org.springframework.web.method.annotation.MethodArgumentTypeMismatchException:
Failed to convert value of type 'java.lang.String' to required type
'java.time.LocalDate'; nested exception is
org.springframework.core.convert.ConversionFailedException: Failed to
convert from type [java.lang.String] to type
[@org.springframework.web.bind.annotation.RequestParam
@org.springframework.format.annotation.DateTimeFormat
java.time.LocalDate] for value '2018-03-22'; nested exception is
java.lang.IllegalArgumentException: Parse attempt failed for value
[2018-03-22] "
问题很明显,所以我找到了解决方案并更改了所有内容。我的方法:
@RequestMapping(path = "reports/daily", method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE)
public String getDailyReport(@RequestParam ("start_date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDate startDate,
@RequestParam("end_date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDate endDate) {
double totalDistance = 0.0;
double totalPrice = 0.0;
List<Transit> transits = transitService.getTransits(startDate, endDate);
for (Transit transit : transits) {
if (transit.getDistance() != null && transit.getPrice() != null) {
try {
totalDistance = totalDistance + transit.getDistance();
totalPrice = totalPrice + transit.getPrice();
} catch (NullPointerException e) {
e.fillInStackTrace().getMessage();
}
}
}
return "Total distance " + totalDistance + ", total price: " + totalPrice;
}
对我来说一切似乎都很好,我还在 pom 文件中添加了所需的依赖项。
我怀疑问题出在这里:
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
根据 the docs DATE_TIME
是日期和时间:
The most common ISO DateTime Format yyyy-MM-dd'T'HH:mm:ss.SSSZ
, e.g. "2000-10-31T01:30:00.000-05:00".
你的 2018-03-22
参数看起来不是这样的。而是使用
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
The most common ISO Date Format yyyy-MM-dd
, e.g. "2000-10-31".
start_date
和 end_date
相同。
我想发送这样的请求:
- http://localhost:8080/reports/daily?start_date=2018-03-22&end_date=2018-03-24
我有一个错误:
"2018-05-31 15:40:29.623 WARN 11496 --- [nio-8080-exec-5] .w.s.m.s.DefaultHandlerExceptionResolver : Failed to bind request element: org.springframework.web.method.annotation.MethodArgumentTypeMismatchException: Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @org.springframework.format.annotation.DateTimeFormat java.time.LocalDate] for value '2018-03-22'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-03-22] "
问题很明显,所以我找到了解决方案并更改了所有内容。我的方法:
@RequestMapping(path = "reports/daily", method = RequestMethod.GET,
consumes = MediaType.APPLICATION_JSON_VALUE)
public String getDailyReport(@RequestParam ("start_date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDate startDate,
@RequestParam("end_date")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
LocalDate endDate) {
double totalDistance = 0.0;
double totalPrice = 0.0;
List<Transit> transits = transitService.getTransits(startDate, endDate);
for (Transit transit : transits) {
if (transit.getDistance() != null && transit.getPrice() != null) {
try {
totalDistance = totalDistance + transit.getDistance();
totalPrice = totalPrice + transit.getPrice();
} catch (NullPointerException e) {
e.fillInStackTrace().getMessage();
}
}
}
return "Total distance " + totalDistance + ", total price: " + totalPrice;
}
对我来说一切似乎都很好,我还在 pom 文件中添加了所需的依赖项。
我怀疑问题出在这里:
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE_TIME)
根据 the docs DATE_TIME
是日期和时间:
The most common ISO DateTime Format
yyyy-MM-dd'T'HH:mm:ss.SSSZ
, e.g. "2000-10-31T01:30:00.000-05:00".
你的 2018-03-22
参数看起来不是这样的。而是使用
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)
The most common ISO Date Format
yyyy-MM-dd
, e.g. "2000-10-31".
start_date
和 end_date
相同。