gpt4 book ai didi

java - Spring Boot 不为 ZonedDateTime 使用自定义反序列化器

转载 作者:行者123 更新时间:2023-11-30 10:13:10 26 4
gpt4 key购买 nike

我正在尝试设置我的 spring 引导配置以使用我的自定义 serializerdeserializer。

反序列化器代码

public class CustomZonedDateTimeDeserializer extends JsonDeserializer<ZonedDateTime> {

@Override
public ZonedDateTime deserialize(JsonParser jsonParser, DeserializationContext context)
throws IOException, JsonProcessingException {
ObjectCodec oc = jsonParser.getCodec();
TextNode node = (TextNode) oc.readTree(jsonParser);
String dateString = node.textValue();
return ZonedDateTime.parse(dateString, CustomZonedDateTimeSerializer.formatter);
}

@Override
public Class<?> handledType() {
return ZonedDateTime.class;
}

序列化器代码

public class CustomZonedDateTimeSerializer extends JsonSerializer<ZonedDateTime> {

@Override
public Class<ZonedDateTime> handledType() {
return ZonedDateTime.class;
}

public final static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ssZ");

@Override
public void serialize(ZonedDateTime date, JsonGenerator generator, SerializerProvider provider)
throws IOException, JsonProcessingException {
final String dateString = date.format(formatter);
generator.writeString(dateString);

}

}

这是我的配置:

@Configuration
public class JacksonConfiguration {

@Autowired
Jackson2ObjectMapperBuilder jacksonBuilder;

@PostConstruct
public void configureJackson() {

final CustomZonedDateTimeSerializer serializer = new CustomZonedDateTimeSerializer();
jacksonBuilder.serializers(serializer);

final CustomZonedDateTimeDeserializer deserializer = new CustomZonedDateTimeDeserializer();
jacksonBuilder.deserializers(deserializer);
}

}

我正在创建一个 REST API,所以我在我的 Controller 中有这个:

@RequestMapping(value = "date", method = { RequestMethod.GET })
@ApiOperation(value = "", notes = "")
public ResponseWrapper<String> testDATE(
@RequestParam @ApiParam(value = "", required = true) ZonedDateTime date) {
System.out.println(date.toString());
return new ResponseWrapper<String>(date.toString());
}

到目前为止我已经尝试过:创建一个自定义模块并将其作为@Bean 添加到配置中,将序列化器和反序列化器添加到配置中的 jackonBuilder 对象,以及类似于此 example 中的代码

我是否遗漏了什么或做错了什么?每次尝试都会出现以下错误:

"Failed to convert value of type 'java.lang.String' to required type 'java.time.ZonedDateTime'; nested exception is org.springframework.core.convert.ConversionFailedException: Failed to convert from type [java.lang.String] to type [@org.springframework.web.bind.annotation.RequestParam @io.swagger.annotations.ApiParam java.time.ZonedDateTime] for value '2018-07-10T00:00:00+0000'; nested exception is java.lang.IllegalArgumentException: Parse attempt failed for value [2018-07-10T00:00:00+0000]"

enter image description here

我每次都发送以下值:2018-07-10T00:00:00+0000,该值甚至没有到达我有断点的反序列化器。

最佳答案

在你的配置类中添加这个 bean:

@Bean
public Jackson2ObjectMapperBuilder configureObjectMapper() {
Jackson2ObjectMapperBuilder builder = new Jackson2ObjectMapperBuilder();
final CustomZonedDateTimeSerializer serializer = new CustomZonedDateTimeSerializer();
builder.serializers(serializer);

final CustomZonedDateTimeDeserializer deserializer = new CustomZonedDateTimeDeserializer();
builder.deserializers(deserializer);
return builder;
}

关于java - Spring Boot 不为 ZonedDateTime 使用自定义反序列化器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51497388/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com