• springboot 拦截器 导致fastjson 大小写失效


    实体类

    public class Info{
        @JsonFeild(“sDqClose”)
    	private String sDqClose;
    	private String sDqHigh;
    }
    
    • 1
    • 2
    • 3
    • 4
    • 5

    拦截器

    
    @Configuration
    @EnableWebMvc
    public class WebMvcConfigurerConfig implements WebMvcConfigurer {
    
        private CorsConfiguration buildConfig() {
            CorsConfiguration corsConfiguration = new CorsConfiguration();
            corsConfiguration.addAllowedOrigin("*");
            corsConfiguration.setAllowCredentials(true);
            corsConfiguration.addAllowedHeader("*");
            corsConfiguration.addAllowedMethod("*");
    
            return corsConfiguration;
        }
    
        @Bean
        public CorsFilter corsFilter() {
            UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();
            source.registerCorsConfiguration("/**", buildConfig());
            return new CorsFilter(source);
        }
    
        @Bean
        CookieInterceptor cookieInterceptor(){
            return new CookieInterceptor();
        }
    
        @Override
        public void addInterceptors(InterceptorRegistry registry) {
        	registry.addInterceptor(cookieInterceptor()).addPathPatterns("/cs/portfolio/**")
        	.addPathPatterns("/dic/**");
        }
      }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33

    Json返回体(大写D变成小写,@JsonFeild 注解失效)

    {
    "sdqClose":"2.3",
    "sdqHigh":"3.4"
    }
    
    • 1
    • 2
    • 3
    • 4

    考虑Springboot拦截器导致FastJson失效,重新配置FastJson序列化

    
     
        @Override
        public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
            // 清除默认 Json 转换器
            converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);
     
            // 配置 FastJson
            FastJsonConfig config = new FastJsonConfig();
            config.setSerializerFeatures(SerializerFeature.QuoteFieldNames,
                    SerializerFeature.WriteEnumUsingToString,
                    SerializerFeature.WriteMapNullValue,
                    SerializerFeature.WriteDateUseDateFormat,
                    SerializerFeature.BrowserCompatible,
                    SerializerFeature.DisableCircularReferenceDetect);
            // 解决long型JS精度丢失问题
            SerializeConfig serializeConfig = SerializeConfig.globalInstance;
            serializeConfig.put(Long.class, ToStringSerializer.instance);
            serializeConfig.put(Long.TYPE, ToStringSerializer.instance);
            config.setSerializeConfig(serializeConfig);
     
            // 添加 FastJsonHttpMessageConverter
            FastJsonHttpMessageConverter fastJsonHttpMessageConverter = new
                    FastJsonHttpMessageConverter();
            fastJsonHttpMessageConverter.setFastJsonConfig(config);
            List<MediaType> fastMediaTypes = new ArrayList<>();
            fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
            fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);
            converters.add(fastJsonHttpMessageConverter);
        }
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30

    成功返回正确Json

    "sDqClose":"2.3",
    "sDqHigh":"3.4"
    
    • 1
    • 2
  • 相关阅读:
    SAP UI5 sap.ui.base.ManagedObject 的构造函数参数讲解
    网络模型的保存与读取
    ubuntu 20.04 Kimera semantic 运行记录
    浅谈兼容性测试的关键步骤
    多模块项目中Mybatis的Mapper内部方法找不到的解决办法
    IP-guard Web系统远程命令执行漏洞说明
    分割等和子集【动态规划】
    GaussDB数据库SQL系列-聚合函数
    牛客·凤凰(https://ac.nowcoder.com/acm/contest/26908/1006)
    CSS 毛玻璃特效运用目录
  • 原文地址:https://blog.csdn.net/weixin_41849346/article/details/134040689