Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Failed to convert value of type 'java.lang.String' to required type 'java.time.LocalDate';

I want to get a date passed as a query parameter to a Spring MVC controller.

I've got this dependency:

<dependency>
    <groupId>com.fasterxml.jackson.datatype</groupId>
    <artifactId>jackson-datatype-jsr310</artifactId>
</dependency>

My URL looks like:

http://localhost:8080/userProducts/2?date=2019-3-29

My controller class looks like:

package trainingapp.userproduct;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.format.annotation.DateTimeFormat;
import org.springframework.web.bind.annotation.*;
import trainingapp.calculations.NutrientsCalculationFacade;
import trainingapp.historysystemofmeals.HistorySystemService;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.List;

@RestController
public class UserProductController {

    private final NutrientsCalculationFacade userProductCalculationFacade;
    private final UserProductFindOperationService userProductFindOperationService;
    private final HistorySystemService historySystemService;

    @Autowired
    public UserProductController(NutrientsCalculationFacade userProductCalculationFacade, UserProductFindOperationService userProductFindOperationService, HistorySystemService historySystemService) {
        this.userProductCalculationFacade = userProductCalculationFacade;
        this.userProductFindOperationService = userProductFindOperationService;
        this.historySystemService = historySystemService;
    }

    //yyyy-MM-dd
    @GetMapping("/userProducts/{userID}")
    public String getAllEatenSummedNutrientsByGivenIDInParticularDay(@PathVariable int userID,
                                                                     @RequestParam("date")
                                                                     @DateTimeFormat(iso = DateTimeFormat.ISO.DATE) LocalDate date) {
        return historySystemService.getAllEatenSummedNutrientsByGivenIDInParticularDay(userID, date);
    }
}

I get the following error:

{
    "timestamp": "2019-03-29T15:22:44.640+0000",
    "status": 400,
    "error": "Bad Request",
    "message": "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 '2019-3-29'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2019-3-29]",
    "path": "/userProducts/2"
}

What should I change or add? I was trying to resolve this problem with How to use LocalDateTime RequestParam in Spring? I get "Failed to convert String to LocalDateTime", but it didnt work.

like image 326
pipilam Avatar asked Mar 29 '19 15:03

pipilam


People also ask

Can we convert string to LocalDate in Java?

Parsing String to LocalDateparse() method takes two arguments. The first argument is the string representing the date. And the second optional argument is an instance of DateTimeFormatter specifying any custom pattern. //Default pattern is yyyy-MM-dd LocalDate today = LocalDate.

How do I cast a string to LocalDateTime?

String str = "2016-03-04 11:30"; DateTimeFormatter formatter = DateTimeFormatter. ofPattern("yyyy-MM-dd HH:mm"); LocalDateTime dateTime = LocalDateTime. parse(str, formatter); Done, that's all is required to convert a formatted String to transform into a LocalDateTime.

What is the default format of LocalDate in Java 8?

LocalDate is an immutable class that represents Date with default format of yyyy-MM-dd.


1 Answers

2019-3-29 is a bad input and the 400 status code is accurate. See the DateTimeFormat.ISO#DATE documentation:

The most common ISO Date Format yyyy-MM-dd, e.g. "2000-10-31".

So, to match the above format, your input should be 2019-03-29 instead.

like image 176
cassiomolin Avatar answered Oct 05 '22 23:10

cassiomolin