0
点赞
收藏
分享

微信扫一扫

解决Feign反序列化LocalDateTime异常

江南北 2021-09-21 阅读 88
  1. 引入Feign的Gson支持
        <dependency>
            <groupId>io.github.openfeign</groupId>
            <artifactId>feign-gson</artifactId>
        </dependency>
  1. 配置Encoder,Decoder序列化类,指定为Gson
@Configuration
public class FeignCodecConfiguration {

    private static final String DEFAULT_DATE_TIME_FORMATTER = "yyyy-MM-dd HH:mm:ss";

    private static final String DEFAULT_DATE_FORMATTER = "yyyy-MM-dd";

    private static final String DEFAULT_TIME_FORMATTER = "HH:mm:ss";

    private static final Gson gson = GSON = new GsonBuilder()
                .serializeNulls()
                .registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (localDateTime, typeOfSrc, context) -> {
                    if (Objects.isNull(localDateTime)) {
                        return new JsonPrimitive("");
                    }
                    return new JsonPrimitive(localDateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMATTER)));
                })
                .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json1, typeOfT, context) -> {
                    String string = json1.getAsJsonPrimitive().getAsString();
                    if (Strings.isNullOrEmpty(string)) {
                        return null;
                    }
                    return LocalDateTime.parse(string, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMATTER));
                })
                .registerTypeAdapter(LocalDate.class, (JsonSerializer<LocalDate>) (localDate, type, jsonSerializationContext) -> {
                    if (Objects.isNull(localDate)) {
                        return new JsonPrimitive("");
                    }
                    return new JsonPrimitive(localDate.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATTER)));
                })
                .registerTypeAdapter(LocalDate.class, (JsonDeserializer<LocalDate>) (json, typeOfT, context) -> {
                    String string = json.getAsJsonPrimitive().getAsString();
                    if (Strings.isNullOrEmpty(string)) {
                        return null;
                    }
                    return LocalDate.parse(string, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATTER));
                })
                .registerTypeAdapter(LocalTime.class, (JsonSerializer<LocalTime>) (localTime, type, jsonSerializationContext) -> {
                    if (Objects.isNull(localTime)) {
                        return new JsonPrimitive("");
                    }
                    return new JsonPrimitive(localTime.format(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMATTER)));
                })
                .registerTypeAdapter(LocalTime.class, (JsonDeserializer<LocalTime>) (json, typeOfT, context) -> {
                    String string = json.getAsJsonPrimitive().getAsString();
                    if (Strings.isNullOrEmpty(string)) {
                        return null;
                    }
                    return LocalTime.parse(string, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMATTER));
                })
                .create();
        PRETTY_GSON = new GsonBuilder()
                .serializeNulls()
                .registerTypeAdapter(LocalDateTime.class, (JsonSerializer<LocalDateTime>) (localDateTime, typeOfSrc, context) -> {
                    if (Objects.isNull(localDateTime)) {
                        return new JsonPrimitive("");
                    }
                    return new JsonPrimitive(localDateTime.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMATTER)));
                })
                .registerTypeAdapter(LocalDateTime.class, (JsonDeserializer<LocalDateTime>) (json1, typeOfT, context) -> {
                    String string = json1.getAsJsonPrimitive().getAsString();
                    if (Strings.isNullOrEmpty(string)) {
                        return null;
                    }
                    return LocalDateTime.parse(string, DateTimeFormatter.ofPattern(DEFAULT_DATE_TIME_FORMATTER));
                })
                .registerTypeAdapter(LocalDate.class, (JsonSerializer<LocalDate>) (localDate, type, jsonSerializationContext) -> {
                    if (Objects.isNull(localDate)) {
                        return new JsonPrimitive("");
                    }
                    return new JsonPrimitive(localDate.format(DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATTER)));
                })
                .registerTypeAdapter(LocalDate.class, (JsonDeserializer<LocalDate>) (json, typeOfT, context) -> {
                    String string = json.getAsJsonPrimitive().getAsString();
                    if (Strings.isNullOrEmpty(string)) {
                        return null;
                    }
                    return LocalDate.parse(string, DateTimeFormatter.ofPattern(DEFAULT_DATE_FORMATTER));
                })
                .registerTypeAdapter(LocalTime.class, (JsonSerializer<LocalTime>) (localTime, type, jsonSerializationContext) -> {
                    if (Objects.isNull(localTime)) {
                        return new JsonPrimitive("");
                    }
                    return new JsonPrimitive(localTime.format(DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMATTER)));
                })
                .registerTypeAdapter(LocalTime.class, (JsonDeserializer<LocalTime>) (json, typeOfT, context) -> {
                    String string = json.getAsJsonPrimitive().getAsString();
                    if (Strings.isNullOrEmpty(string)) {
                        return null;
                    }
                    return LocalTime.parse(string, DateTimeFormatter.ofPattern(DEFAULT_TIME_FORMATTER));
                })
                .setPrettyPrinting()
                .create();

    @Bean
    public GsonDecoder gsonDecoder() {
        return new GsonDecoder(gson);
    }

    @Bean
    public GsonEncoder gsonEncoder() {
        return new GsonEncoder(gson);
    }
}
举报

相关推荐

0 条评论