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

原文 标签 java spring spring-boot jackson deserialization

我正在尝试设置我的 Spring Boot 配置以使用我的自定义 序列化器 解串器。

解串器代码

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/

相关文章:

java - 使用多对一映射编写插入 HQL 查询

java - 在@OnTransition 注解的方法中获取StateContext

java - 一次失败后,Spring BatchSqlUpdate不执行其余的SQL

java - Spring Boot 运行时添加 Controller ?

java - 如何使用存储库模式更新 mongodb 中的嵌入文档

java - 从servlet以自动增量方式在oracle中获取记录

java - 不允许非成员(member)显示 jsp 页面

java - 如何将2D ArrayList转换为2D字符串?

java - 没有@EnableAutoConfiguration的Spring Actuator指标存在问题

java - MySQL和Spring Boot