当前位置:首页 > 编程笔记 > 正文
已解决

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

来自网友在路上 160860提问 提问时间:2023-10-27 13:36:05阅读次数: 60

最佳答案 问答题库608位专家为你答疑解惑

实体类

public class Info{@JsonFeild(“sDqClose”)private String sDqClose;private String sDqHigh;
}

拦截器


@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;}@Beanpublic CorsFilter corsFilter() {UrlBasedCorsConfigurationSource source = new UrlBasedCorsConfigurationSource();source.registerCorsConfiguration("/**", buildConfig());return new CorsFilter(source);}@BeanCookieInterceptor cookieInterceptor(){return new CookieInterceptor();}@Overridepublic void addInterceptors(InterceptorRegistry registry) {registry.addInterceptor(cookieInterceptor()).addPathPatterns("/cs/portfolio/**").addPathPatterns("/dic/**");}}

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

{
"sdqClose":"2.3",
"sdqHigh":"3.4"
}

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

@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// 清除默认 Json 转换器converters.removeIf(converter -> converter instanceof MappingJackson2HttpMessageConverter);// 配置 FastJsonFastJsonConfig 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);// 添加 FastJsonHttpMessageConverterFastJsonHttpMessageConverter fastJsonHttpMessageConverter = newFastJsonHttpMessageConverter();fastJsonHttpMessageConverter.setFastJsonConfig(config);List<MediaType> fastMediaTypes = new ArrayList<>();fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);fastJsonHttpMessageConverter.setSupportedMediaTypes(fastMediaTypes);converters.add(fastJsonHttpMessageConverter);}

成功返回正确Json

"sDqClose":"2.3",
"sDqHigh":"3.4"
查看全文

99%的人还看了

猜你感兴趣

版权申明

本文"springboot 拦截器 导致fastjson 大小写失效":http://eshow365.cn/6-26061-0.html 内容来自互联网,请自行判断内容的正确性。如有侵权请联系我们,立即删除!