【问题标题】:String query param to LocalDateTime deserialisation does not work after upgrade from spring boot 2.2 to 3从 spring boot 2.2 升级到 3 后,LocalDateTime 反序列化的字符串查询参数不起作用
【发布时间】:2022-11-26 04:00:26
【问题描述】:

将 spring-boot 从 2.2 升级到 3.0 后,LocalDateTime 查询参数的反序列化开始失败。

这是控制器的签名:

public Mono<QueryResult> index(@PathVariable String breakdown, QueryParams queryParams)

QueryParams 在哪里

@Data
public class QueryParams {

    @NotBlank
    @DateTimeFormat(pattern = "YYYYMMddHHmmss")
    private LocalDateTime from;

    @NotBlank
    @DateTimeFormat(pattern = "YYYYMMddHHmmss")
    private LocalDateTime to;

此代码在 spring boot 2.2 中运行良好但在 3.0 中抛出:

2022-11-25 18:33:33 http-nio-8080-exec-8 - org.springframework.web.servlet.mvc.support.DefaultHandlerExceptionResolver WARN Resolved [org.springframework.validation.BindException: org.springframework.validation.BeanPropertyBindingResult: 2 errors<EOL>Field error in object 'queryParams' on field 'from': rejected value [20221001000000]; codes [typeMismatch.queryParams.from,typeMismatch.from,typeMismatch.org.joda.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [queryParams.from,from]; arguments []; default message [from]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.LocalDateTime' for property 'from'; Cannot convert value of type 'java.lang.String' to required type 'org.joda.time.LocalDateTime' for property 'from': no matching editors or conversion strategy found]<EOL>Field error in object 'queryParams' on field 'to': rejected value [20221031000000]; codes [typeMismatch.queryParams.to,typeMismatch.to,typeMismatch.org.joda.time.LocalDateTime,typeMismatch]; arguments [org.springframework.context.support.DefaultMessageSourceResolvable: codes [queryParams.to,to]; arguments []; default message [to]]; default message [Failed to convert property value of type 'java.lang.String' to required type 'org.joda.time.LocalDateTime' for property 'to'; Cannot convert value of type 'java.lang.String' to required type 'org.joda.time.LocalDateTime' for property 'to': no matching editors or conversion strategy found]]

【问题讨论】:

    标签: spring-boot


    【解决方案1】:

    试一试,这对我有用。

    public class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
            @Override
            public LocalDateTime deserialize(JsonParser arg0, DeserializationContext arg1) throws IOException, JacksonException {
                return LocalDateTime.parse(arg0.getText());
            }
        }
    
    public class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
        @Override
        public void serialize(LocalDateTime arg0, JsonGenerator arg1, SerializerProvider arg2) throws IOException {
            arg1.writeString(arg0.toString());
        }
    
    }
    
    
    
    @JsonSerialize(using = LocalDateTimeSerializer.class)
        @JsonDeserialize(using = LocalDateTimeDeserializer.class)
        protected LocalDateTime updatedTime;
    

    【讨论】:

      猜你喜欢
      • 2018-05-30
      • 2010-11-08
      • 2023-02-04
      • 2021-09-13
      • 1970-01-01
      • 1970-01-01
      • 2019-04-16
      • 1970-01-01
      • 2017-02-10
      相关资源
      最近更新 更多