[英]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.我从 Postman 向 spring 应用程序发送表单数据请求。 The LocalDate field in the Spring application as a text field from the form-data. Spring 应用程序中的 LocalDate 字段作为表单数据中的文本字段。 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]; object 'vehicleDto' 字段 'leasingExpiry' 中的字段错误:拒绝值 [2021-01-01]; codes [typeMismatch.vehicleDto.leasingExpiry,typeMismatch.leasingExpiry,typeMismatch.java.time.LocalDate,typeMismatch];代码 [typeMismatch.vehicleDto.leasingExpiry,typeMismatch.leasingExpiry,typeMismatch.java.time.LocalDate,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [vehicleDto.leasingExpiry,leasingExpiry]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable:代码 [vehicleDto.leasingExpiry,leasingExpiry]; arguments []; arguments []; default message [leasingExpiry]];默认消息 [leasingExpiry]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'java.time.LocalDate' for property 'leasingExpiry';默认消息 [无法将类型“java.lang.String”的属性值转换为属性“leasingExpiry”所需的类型“java.time.LocalDate”; 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';嵌套异常是 org.springframework.core.convert.ConversionFailedException: 无法从类型 [java.lang.String] 转换为类型 [@javax.validation.constraints.NotBlank java.time.LocalDate] 值'2021-01-01 '; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2021-01-01]]嵌套异常是 java.lang.IllegalArgumentException:值解析尝试失败 [2021-01-01]]
As far as I understood, you have a problem with mapping String
object into LocalDate
object in Spring controller.据我了解,您在将String
object 映射到LocalDate
object 时遇到问题 Spring Z594C103F2C6E04C31DAZ05FAZAB1。 Try annotate LocalDate
field with following annotations:尝试使用以下注释注释LocalDate
字段:
@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.有一天我遇到了同样的问题,它解决了我的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.