DateTimeParseException

DateTimeParseException_第1张图片

前端请求为字符串的时间格式2023-02-16 19:19:51,服务端用LocalDateTime类型接收时报解析异常

java.time.format.DateTimeParseException: Text '2023-02-16 19:19:51' could not be parsed at index 10

方法一:

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

private LocalDateTime time;

方法二:

package a;

import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;

import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;

import java.time.LocalDateTime;

import java.time.format.DateTimeFormatter;

import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;

import org.springframework.context.annotation.Bean;

import org.springframework.context.annotation.Configuration;

@Configuration

public class JacksonConfig {

private static final DateTimeFormatter DATETIME_FORMAT = DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss");

@Bean

public Jackson2ObjectMapperBuilderCustomizer jsonCustomizer() {

return builder -> {

builder.failOnEmptyBeans(false);

builder.failOnUnknownProperties(false);

LocalDateTimeDeserializer deserializer = new LocalDateTimeDeserializer(DATETIME_FORMAT);

LocalDateTimeSerializer serializer = new LocalDateTimeSerializer(DATETIME_FORMAT);

builder.serializerByType(LocalDateTime.class, serializer);

builder.deserializerByType(LocalDateTime.class, deserializer);

};

}

}

你可能感兴趣的:(spring,时间解析异常,LocalDateTime异常,json反序列化时间异常)