Skip to content
Advertisement

Conversion Error on LocalDate attribute when form-data request is sent to a Spring boot application

I send a form-data request from the Postman to a spring application. The LocalDate field in the Spring application as a text field from the form-data. I got a Conversion Error.

@Column(
    name = "leasing_expiry",
    nullable = false
)
private LocalDate leasingExpiry;

Field error in object ‘vehicleDto’ on field ‘leasingExpiry’: rejected value [2021-01-01]; codes [typeMismatch.vehicleDto.leasingExpiry,typeMismatch.leasingExpiry,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [vehicleDto.leasingExpiry,leasingExpiry]; arguments []; default message [leasingExpiry]]; default message [Failed to convert property value of type ‘java.lang.String’ to required type ‘java.time.LocalDate’ for property ‘leasingExpiry’; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@javax.validation.constraints.NotBlank java.time.LocalDate] for value ‘2021-01-01’; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2021-01-01]]

Postman Request

Advertisement

Answer

As far as I understood, you have a problem with mapping String object into LocalDate object in Spring controller. Try annotate LocalDate field with following annotations:

@JsonFormat(shape = JsonFormat.Shape.STRING, pattern = "yyyy-MM-dd")
@DateTimeFormat(iso = DateTimeFormat.ISO.DATE)

and following dependency might be required:

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.11.1</version>
</dependency>

I was facing with the same problem some day and it solved my issue.

User contributions licensed under: CC BY-SA
2 People found this is helpful
Advertisement