搜索
您的当前位置:首页正文

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

来源:独旅网

1、问题
前端传值

{
    "lId":"1"
}

后端同样的字段接收不到

{
    "lid":"1"
}


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);
    }
}

因篇幅问题不能全部显示,请点此查看更多更全内容

Top