美文网首页
Jsckson序列化出错问题

Jsckson序列化出错问题

作者: 无我_无他_有你 | 来源:发表于2021-04-26 16:05 被阅读0次

序言:
  前后端联调时,我后台是用对象接收参数的,这时候出现了一个问题,前端少传了参数没问题,但是多传了字段就报400 参数异常错误。一番了解时发现,这是因为spring默认是开启了序列化属性校验,所以我要让spring对多出的参数进行忽略,这样就解决问题了。

经过一番资料查询,发现解决办法有两个
一、yml文件中进行配置

spring:
  jackson:
    #日期格式化
    date-format: yyyy-MM-dd HH:mm:ss
    serialization:
       #格式化输出 
      indent_output: true
      #忽略无法转换的对象
      fail_on_empty_beans: false
    #设置空如何序列化
    defaultPropertyInclusion: NON_EMPTY
    deserialization:
      #允许对象忽略json中不存在的属性
      fail_on_unknown_properties: false
    parser:
      #允许出现特殊字符和转义符
      allow_unquoted_control_chars: true
      #允许出现单引号
      allow_single_quotes: true

但是不知道为什么,我配置了 serialization:spring.jackson.fail_on_unknown_properties=false这一项还是没有效果,多传参数依然报错,有知道的大佬请不吝赐教!抱拳~

二、配置文件配置

jacksonObjectMapper.java

package com.tianshu.evp.orderService.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.format.DateTimeFormatter;

/**
 * @Author: wqf2021/04/26
 * @Date: 2021/04/26
 * @Description:
 */
@Configuration
public class JacksonConfig {

    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        //时间序列化
        builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //针对于JDK新时间类。序列化时带有T的问题,自定义格式化字符串
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        //LocalDateTime
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        //LocalDate
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        //LocalTime
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        builder.modules(javaTimeModule);
       //序列化将BigDecimal转String类型 BigDecimal数据返回到前端会出现精度丢失的问题
        builder.featuresToEnable(JsonGenerator.Feature.WRITE_BIGDECIMAL_AS_PLAIN);
        //Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        //未知字段不序列化
        builder.failOnUnknownProperties(false);
        //允许出现特殊字符和转义符
        builder.featuresToEnable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
        // 允许出现单引号
        builder.featuresToEnable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
        return builder.createXmlMapper(false).build();
    }
}

使用配置文件的方式,解决了问题。
开发中,配置时间序列化转换,未知字段不序列化,null值不序列化(减少数据传输),差不多够用了,如果还有为null的数组要序列化为[]空数组的等业务需求,自己在配上就好了。

有问题,还请各位提出,大家一起进步!
最后提供另一位大佬的博链接:https://www.cnblogs.com/scar1et/articles/14134024.html

2021/4/30 内容补充

package com.tianshu.evp.appservice.common.config;

import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.databind.DeserializationContext;
import com.fasterxml.jackson.databind.JsonDeserializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;
import com.tianshu.evp.commons.domain.excption.BusinessException;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import java.io.IOException;
import java.time.*;
import java.time.format.DateTimeFormatter;

/**
 * @Author: wqf2021/04/26
 * @Date: 2021/04/26
 * @Description:
 */
@Configuration
public class JacksonConfig {
    @Bean
    @Primary
    @ConditionalOnMissingBean(ObjectMapper.class)
    public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
        //时间序列化
        builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");
        //针对于JDK新时间类。序列化时带有T的问题,自定义格式化字符串
        JavaTimeModule javaTimeModule = new JavaTimeModule();
        //LocalDateTime 
        javaTimeModule.addSerializer(LocalDateTime.class, new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalDateTime.class, new LocalDateTimeDeserializer());
        //LocalDate
        javaTimeModule.addSerializer(LocalDate.class, new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
        javaTimeModule.addDeserializer(LocalDate.class, new LocalDateDeserializer());
        //LocalTime
        javaTimeModule.addSerializer(LocalTime.class, new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        javaTimeModule.addDeserializer(LocalTime.class, new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
        builder.modules(javaTimeModule);

        //Include.NON_EMPTY 属性为 空("") 或者为 NULL 都不序列化,则返回的json是没有这个字段的。这样对移动端会更省流量
        builder.serializationInclusion(JsonInclude.Include.NON_NULL);
        //未知字段不序列化
        builder.failOnUnknownProperties(false);
        //允许出现特殊字符和转义符
        //builder.featuresToEnable(JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS);
        // 允许出现单引号
        //builder.featuresToEnable(JsonParser.Feature.ALLOW_SINGLE_QUOTES);
        return builder.createXmlMapper(false).build();
    }

    //    @Bean
    //    public Jackson2ObjectMapperBuilderCustomizer jackson2ObjectMapperBuilderCustomizer() {
    //        return builder -> {
    //            builder.serializerByType(LocalDateTime.class, new LocalDateTimeSerializer());
    //            builder.deserializerByType(LocalDateTime.class, new LocalDateTimeDeserializer());
    //        };
    //    }

    //    /**
    //     * 序列化
    //     */
    //    public static class LocalDateTimeSerializer extends JsonSerializer<LocalDateTime> {
    //        @Override
    //        public void serialize(LocalDateTime value, JsonGenerator gen, SerializerProvider serializers)
    //                throws IOException {
    //            if (value != null) {
    //                long timestamp = value.atZone(ZoneId.systemDefault()).toInstant().toEpochMilli();
    //                gen.writeNumber(timestamp);
    //            }
    //        }
    //    }

    /**
     * LocalDateTime反序列化 时间格式匹配
     */
    public static class LocalDateTimeDeserializer extends JsonDeserializer<LocalDateTime> {
        @Override
        public LocalDateTime deserialize(JsonParser p, DeserializationContext deserializationContext)
                throws IOException {
            String value = p.getValueAsString();
            if (value.length() == 19) {
                //传入时间格式为 2020-01-01 00:00:00;
                return LocalDateTime.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss"));
            } else if (value.length() == 13) {
                //传入时间为时间戳
                return Instant.ofEpochMilli(p.getValueAsLong()).atZone(ZoneOffset.ofHours(8)).toLocalDateTime();
            } else {
                throw new BusinessException("请传入正确的时间格式!");
            }
        }
    }

    /**
     * LocalDate反序列化
     */
    public static class LocalDateDeserializer extends JsonDeserializer<LocalDate> {
        @Override
        public LocalDate deserialize(JsonParser p, DeserializationContext deserializationContext)
                throws IOException {
            String value = p.getValueAsString();
            //判断时间格式 2020-01-01
            if (value.length() == 10) {
                //传入时间格式为 2020-01-01 00:00:00;
                return LocalDate.parse(value, DateTimeFormatter.ofPattern("yyyy-MM-dd"));
            } else if (value.length() == 13) {
                //传入时间为时间戳
                return Instant.ofEpochMilli(p.getValueAsLong()).atZone(ZoneOffset.ofHours(8)).toLocalDate();
            } else {
                throw new BusinessException("请传入正确的时间格式!");
            }
        }
    }

}

相关文章

  • Jsckson序列化出错问题

    序言:前后端联调时,我后台是用对象接收参数的,这时候出现了一个问题,前端少传了参数没问题,但是多传了字段就报400...

  • 项目中使用drf的关键步骤

    django的view实现商品列表页 此种方法存在的问题: 工作量大,比较容易出错 日期字段序列化会出错 djan...

  • Lambda表达式捕获与非捕获

    问题起源 下面的相似代码执行过程中,有些会报错,有些真确,想搞懂原因 两种出错的异常如下 问题原因 是否可序列化的...

  • com.esotericsoftware.kryo.kryoex

    这次不扯蛋,直接开讲。 该问题的本质是序列化问题!!!序列化问题!!!序列化问题!!! 重要问题说三遍。 把对象转...

  • Parcelable encountered IOExcepti

    在Android项目里对一个对象进行序列化出错了,日志提示: Java.lang.RuntimeException...

  • Object and no properties discove

    对象为空时,jackson序列化出错。 解决方案一 application.yml中增加spring.jackso...

  • Java序列化心得(二):自定义序列化

    正如前文《Java序列化心得(一):序列化设计和默认序列化格式的问题》中所提到的,默认序列化方法存在各种各样的问题...

  • Java序列化与反序列化

    问题 Java序列化与反序列化是什么?为什么需要序列化与反序列化?有什么好处?如何实现Java序列化与反序列化? ...

  • jackson源码分析

    背景:之前碰到序列化的问题,spring默认序列化又是jackson序列化,然后近来又爆出fastjson序列化的...

  • 【Flink 精选】常见的性能问题及其定位思路

    常见的性能问题及其定位思路 1.常见的性能问题 (1)JSON序列化和反序列化 source的序列化,sink的反...

网友评论

      本文标题:Jsckson序列化出错问题

      本文链接:https://www.haomeiwen.com/subject/xtserltx.html