Java 将表单数据请求发送到Spring启动应用程序时,LocalDate属性发生转换错误
我从邮递员向spring应用程序发送表单数据请求。Spring应用程序中的LocalDate字段作为表单数据中的文本字段。我有一个转换错误Java 将表单数据请求发送到Spring启动应用程序时,LocalDate属性发生转换错误,java,spring,spring-boot,Java,Spring,Spring Boot,我从邮递员向spring应用程序发送表单数据请求。Spring应用程序中的LocalDate字段作为表单数据中的文本字段。我有一个转换错误 @Column( name = "leasing_expiry", nullable = false ) private LocalDate leasingExpiry; 字段“租赁到期日”上的对象“vehicleDto”中的字段错误:拒绝值[2021-01-01];代码[typeMismatch.vehicleDto.
@Column(
name = "leasing_expiry",
nullable = false
)
private LocalDate leasingExpiry;
字段“租赁到期日”上的对象“vehicleDto”中的字段错误:拒绝值[2021-01-01];代码[typeMismatch.vehicleDto.LeasingExpire,typeMismatch.LeasingExpire,typeMismatch.java.time.LocalDate,typeMismatch];参数[org.springframework.context.support.DefaultMessageSourceResolvable:代码[vehicleDto.leasingExpire,leasingExpire];参数[];默认消息[leasingExpire];默认消息[未能将类型为'java.lang.String'的属性值转换为属性'LeasingExpirement'所需的类型'java.time.LocalDate';嵌套异常为org.springframework.core.convert.ConversionFailedException:未能从类型[java.lang.String]转换为类型[@javax.validation.constraints.NotBlank java.time.LocalDate]对于值“2021-01-01”;嵌套异常为java.lang.IllegalArgumentException:对值[2021-01-01]]的分析尝试失败
据我所知,在Spring controller中将
String
对象映射到LocalDate
对象时出现问题。尝试用以下注释注释LocalDate
字段:
@JsonFormat(shape=JsonFormat.shape.STRING,pattern=“yyyy-MM-dd”)
@DateTimeFormat(iso=DateTimeFormat.iso.DATE)
可能需要以下依赖项:
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.11.1</version>
</dependency>
com.fasterxml.jackson.core
杰克逊数据绑定
2.11.1
有一天我遇到了同样的问题,它解决了我的问题。将Jackson依赖项添加到应用程序中,并在您的属性上使用@JsonFormat(shape=JsonFormat.shape.STRING,@JsonFormat(shape=JsonFormat.shape.STRING,pattern=“yyyy-MM-dd”))注释。@DateTimeFormat(pattern=“yyy-MM-dd”)添加此注释也有效。