Spring Boot-无法从类型[java.lang.String]转换为RequestParam类型

时间:2018-07-23 13:52:00

标签: spring spring-boot

在我的控制器中,我有一个端点:

  @GetMapping(value = SUMMARY_URL, produces = "application/json")
    public DailyReportSummary getSummaryOfDailyReports(
            @RequestParam(name = "from", required = false,defaultValue = "10-10-2017 ") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDateTime from,
            @RequestParam(name = "to", required = false,defaultValue = "10-10-2019 ") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDateTime to) {

        List<DailyReport> summary = statisticService.findByDateToSummary(from, to);

        DailyReportSummary dailyReportSummary = new DailyReportSummary(summary);

我虽然一切都很好,但是我有这个错误:

   There was an unexpected error (type=Bad Request, status=400).
    Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDateTime'; 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.LocalDateTime] for value '10-10-2017 '; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [10-10-2017 ]

这有什么问题?我尝试解决此问题,但没有任何效果。

编辑:删除的iso仍然错误:(

1 个答案:

答案 0 :(得分:1)

由于模式dd-MM-yyyy没有时间,您需要使用LocalDate

@GetMapping(value = SUMMARY_URL, produces = "application/json")
public DailyReportSummary getSummaryOfDailyReports(
        @RequestParam(name = "from", required = false, defaultValue = "10-10-2017") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate from,
        @RequestParam(name = "to", required = false, defaultValue = "10-10-2019") @DateTimeFormat(pattern = "dd-MM-yyyy") LocalDate to) {