简体   繁体   English

当表单数据请求发送到 Spring 引导应用程序时,LocalDate 属性上的转换错误

[英]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]]

Postman Request Postman 请求

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.

相关问题 将多部分/表单数据传输到 spring 引导时出现不支持的错误 - Unsupported error while transfering multipart/form-data to spring boot Spring 引导多部分/表单数据请求文件流式传输到下游服务 - Spring Boot multipart/form-data request file streaming to downstream service 405 错误:带有 Spring 的多部分/表单数据 - 405 Error : multipart/form-data with Spring Spring无法处理“多部分/表单数据” POST请求(错误400“错误请求”) - Spring cannot process “multipart/form-data” POST request (error 400 “Bad request”) 从Spring Boot使用multipart / form-data调用外部api - Calling external api from spring boot with multipart/form-data 如何在 Spring 引导中通过 REST API 使用请求正文(JSON)以及多个文件(多部分/表单数据)? - How to consume Request Body (JSON) along with multiple files (multipart/form-data) via REST API in Spring Boot? 解决 spring 过滤器中的多部分/表单数据请求 - Resolving multipart/form-data request in spring filter Spring POST multipart/form-data,请求部分始终为空 - Spring POST multipart/form-data, request parts always empty 表单数据中的POSTMAN请求 - POSTMAN Request in form-data 提交multipart / form-data时Servlet请求contenttype = null - Servlet request contenttype = null when submitting multipart/form-data
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM