SpringBoot默认Jackson解析json大小写转换问题导致前端传值后端接收不到

1、问题
前端传值

{
    "lId":"1"
}

后端同样的字段接收不到
在这里插入图片描述
原因,springboot默认的json解析器带来的问题,会把大小转换为小写
如果将前端传值都改成小写

{
    "lid":"1"
}

在这里插入图片描述
打开springboot的源码可以看到,如果我们没有自定义json解析器,则springboot会创建一个默认的
在这里插入图片描述
解决方案,创建一个自定义的json解析器


import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.*;
import com.fasterxml.jackson.databind.module.SimpleModule;
import org.apache.commons.lang3.StringEscapeUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.IOException;
import java.text.SimpleDateFormat;
import java.util.TimeZone;

public class JacksonUtils extends ObjectMapper{
    private static final Logger log = LoggerFactory.getLogger(JacksonUtils.class);
    private static final JacksonUtils intance = new JacksonUtils();

    public static JacksonUtils getInstance() {
        return intance;
    }

    private JacksonUtils() {
        this(JsonInclude.Include.NON_NULL);
    }

    private JacksonUtils(JsonInclude.Include include) {
        if (include != null) {
            this.setSerializationInclusion(include);
        }

        this.enable(new JsonParser.Feature[]{JsonParser.Feature.ALLOW_SINGLE_QUOTES});
        this.enable(new JsonParser.Feature[]{JsonParser.Feature.ALLOW_UNQUOTED_CONTROL_CHARS});
        this.enable(new JsonParser.Feature[]{JsonParser.Feature.ALLOW_UNQUOTED_FIELD_NAMES});
        this.enable(new JsonParser.Feature[]{JsonParser.Feature.ALLOW_NUMERIC_LEADING_ZEROS});
        this.enable(new JsonParser.Feature[]{JsonParser.Feature.ALLOW_NON_NUMERIC_NUMBERS});
        this.enable(new JsonParser.Feature[]{JsonParser.Feature.ALLOW_COMMENTS});
        this.enable(new JsonGenerator.Feature[]{JsonGenerator.Feature.AUTO_CLOSE_JSON_CONTENT});
        this.enable(new JsonParser.Feature[]{JsonParser.Feature.STRICT_DUPLICATE_DETECTION});
        this.enable(new MapperFeature[]{MapperFeature.ACCEPT_CASE_INSENSITIVE_PROPERTIES});
        this.enable(DeserializationFeature.ACCEPT_EMPTY_ARRAY_AS_NULL_OBJECT);
        this.disable(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES);
        this.disable(SerializationFeature.FAIL_ON_EMPTY_BEANS);
        this.disable(SerializationFeature.WRITE_NULL_MAP_VALUES);
        this.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
        this.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
            public void serialize(Object value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
                jgen.writeString("");
            }
        });
        this.registerModule((new SimpleModule()).addSerializer(String.class, new JsonSerializer<String>() {
            public void serialize(String value, JsonGenerator jgen, SerializerProvider provider) throws IOException, JsonProcessingException {
                jgen.writeString(StringEscapeUtils.unescapeHtml4(value));
            }
        }));
        this.setTimeZone(TimeZone.getDefault());
        this.setDateFormat(new SimpleDateFormat("yyyy-MM-dd HH:mm:ss"));
    }
}

覆盖默认的json解析器

import lombok.extern.slf4j.Slf4j;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;

import java.util.List;

@Slf4j
@Configuration
public class WebMVCConfig extends WebMvcConfigurationSupport {

    /**
     * 自定义输出json格式
     */
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        MappingJackson2HttpMessageConverter jsonConverter = new MappingJackson2HttpMessageConverter();
        jsonConverter.setObjectMapper(JacksonUtils.getInstance());
        converters.add(jsonConverter);
        super.addDefaultHttpMessageConverters(converters);
    }
}
  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值