当前位置: 首页 > news >正文

封面型网站布局做外贸手机网站

封面型网站布局,做外贸手机网站,海报制作app,wordpress登录后台空白文章目录 错误描述问题分析打印目前所有的消息处理器寻找适配版本消息解释器加载顺序 错误原因正确写法使用最新版本fastjson(2024-1-22)配置fastjson2消息转换器(保留系统原消息转换器)替换消息转换器配置fastjson2 错误描述 采用Bean的方式配置FastJsonHttpMessageConverter…

文章目录

    • 错误描述
    • 问题分析
      • 打印目前所有的消息处理器
      • 寻找适配版本
      • 消息解释器加载顺序
    • 错误原因
    • 正确写法
      • 使用最新版本fastjson(2024-1-22)
      • 配置fastjson2消息转换器(保留系统原消息转换器)
      • 替换消息转换器配置fastjson2

错误描述

采用@Bean的方式配置FastJsonHttpMessageConverter消息解释器,实测在【SpringBoot2.6.13】未生效
但是在【SpringBoot2.1.4.RELEASE】版本中正常
2.1.4.RELEASE中引入如下

        <dependency><groupId>com.alibaba</groupId><artifactId>fastjson</artifactId><version>1.2.56</version></dependency>

配置如下

import com.alibaba.fastjson.serializer.SerializeConfig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;@Beanpublic HttpMessageConverters fastJsonHttpMessageConverters() {FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();FastJsonConfig fastJsonConfig = new FastJsonConfig();fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");fastJsonConfig.setSerializerFeatures(SerializerFeature.PrettyFormat,SerializerFeature.QuoteFieldNames,SerializerFeature.WriteEnumUsingName,SerializerFeature.DisableCircularReferenceDetect);List<MediaType> fastMediaTypes = new ArrayList<>();fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);fastConverter.setSupportedMediaTypes(fastMediaTypes);fastConverter.setFastJsonConfig(fastJsonConfig);return new HttpMessageConverters(fastConverter);}

问题分析

打印目前所有的消息处理器

通过打印消息处理器,发现配置并未成功

public class MvcConfig implements WebMvcConfigurer {@Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {for (HttpMessageConverter<?> messageConverter : converters) {System.out.println(messageConverter);}}
}

得到如下输出日志

org.springframework.http.converter.ByteArrayHttpMessageConverter@1ddf42dd
org.springframework.http.converter.StringHttpMessageConverter@5c1c9881
org.springframework.http.converter.ResourceHttpMessageConverter@1c18ee69
org.springframework.http.converter.ResourceRegionHttpMessageConverter@2f99d8c
org.springframework.http.converter.xml.SourceHttpMessageConverter@65d7eea4
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@5d37aa0f
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@6076c66
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@485c84d7

寻找适配版本

官网可知,fastjson早已停止更新,新版本需使用fastjson2
在 Spring 中集成 Fastjson2 | fastjson2 (alibaba.github.io)
引入如下

<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension-spring5</artifactId><version>2.0.43</version>
</dependency>

官网得到如下配置

@Configuration
@EnableWebMvc
public class WebMvcConfigurer extends WebMvcConfigurerAdapter {@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();//自定义配置...FastJsonConfig config = new FastJsonConfig();config.setDateFormat("yyyy-MM-dd HH:mm:ss");config.setReaderFeatures(JSONReader.Feature.FieldBased, JSONReader.Feature.SupportArrayToBean);config.setWriterFeatures(JSONWriter.Feature.WriteMapNullValue, JSONWriter.Feature.PrettyFormat);converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);converter.setSupportedMediaTypes(Collections.singletonList(MediaType.APPLICATION_JSON));converters.add(0, converter);}
}

消息解释器加载顺序

需要将FastJsonHttpMessageConverter配置为第一位消息处理器才能得到输出
其测试过程如下

converters.add(converter);

此时得到输出为

org.springframework.http.converter.ByteArrayHttpMessageConverter@1ddf42dd
org.springframework.http.converter.StringHttpMessageConverter@5c1c9881
org.springframework.http.converter.ResourceHttpMessageConverter@1c18ee69
org.springframework.http.converter.ResourceRegionHttpMessageConverter@2f99d8c
org.springframework.http.converter.xml.SourceHttpMessageConverter@65d7eea4
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@5d37aa0f
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@6076c66
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@485c84d7
com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@1224e1b6

实测未生效

{"code": 200,"msg": "请求成功","data": {"loginName": null,"userName": "柒杉","userCode": null,"loginDate": 1705547281595}
}

修改为

converters.add(0, converter);

得到输出

com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter@2a667f44
org.springframework.http.converter.ByteArrayHttpMessageConverter@53ba7997
org.springframework.http.converter.StringHttpMessageConverter@3f6f9cef
org.springframework.http.converter.ResourceHttpMessageConverter@61dd1c3d
org.springframework.http.converter.ResourceRegionHttpMessageConverter@7858d31d
org.springframework.http.converter.xml.SourceHttpMessageConverter@782e6b40
org.springframework.http.converter.support.AllEncompassingFormHttpMessageConverter@3b65084e
org.springframework.http.converter.xml.Jaxb2RootElementHttpMessageConverter@32d0d7eb
org.springframework.http.converter.json.MappingJackson2HttpMessageConverter@cae2a97

错误原因

在高版本中需要采用最新的fastjson2配置

正确写法

使用最新版本fastjson(2024-1-22)

<dependency><groupId>com.alibaba.fastjson2</groupId><artifactId>fastjson2-extension-spring5</artifactId><version>2.0.43</version>
</dependency>

配置fastjson2消息转换器(保留系统原消息转换器)

        @Overridepublic void extendMessageConverters(List<HttpMessageConverter<?>> converters) {// 创建 FastJson 的消息转换器实例FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();// 创建 FastJson 的配置实例FastJsonConfig config = new FastJsonConfig();// 设置时间类型日期格式config.setDateFormat("yyyy-MM-dd HH:mm:ss");config.setReaderFeatures(// 基于字段序列化,如果不配置,会默认基于public的field和getter方法序列化。// 配置后,会基于非static的field(包括private)做序列化。JSONReader.Feature.FieldBased,// 支持数据映射的方式JSONReader.Feature.SupportArrayToBean);config.setWriterFeatures(// 显示null与空字符串//                JSONWriter.Feature.WriteMapNullValue,// 格式化输出JSONWriter.Feature.PrettyFormat,// 将Long序列化为StringJSONWriter.Feature.WriteLongAsString);// 将序列化配置设置到 FastJson 配置中converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);// 创建一个媒体类型,表示支持 JSON 格式,并使用 UTF-8 编码List<MediaType> fastMediaTypes = new ArrayList<>();MediaType utf8MediaType = new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8);fastMediaTypes.add(utf8MediaType);converter.setSupportedMediaTypes(fastMediaTypes);// 将 FastJson 消息转换器添加到 Spring Boot 的消息转换器列表中,位置是第一个,这样确保它优先于其他消息转换器converters.add(0, converter);}

替换消息转换器配置fastjson2

@Overridepublic void configureMessageConverters(List<HttpMessageConverter<?>> converters) {// 创建 FastJson 的消息转换器实例FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();// 创建 FastJson 的配置实例FastJsonConfig config = new FastJsonConfig();// 设置时间类型日期格式config.setDateFormat("yyyy-MM-dd HH:mm:ss");config.setReaderFeatures(// 基于字段序列化,如果不配置,会默认基于public的field和getter方法序列化。// 配置后,会基于非static的field(包括private)做序列化。JSONReader.Feature.FieldBased,// 支持数据映射的方式JSONReader.Feature.SupportArrayToBean);config.setWriterFeatures(// 显示null与空字符串//                JSONWriter.Feature.WriteMapNullValue,// 格式化输出JSONWriter.Feature.PrettyFormat,// 将Long序列化为StringJSONWriter.Feature.WriteLongAsString);// 将序列化配置设置到 FastJson 配置中converter.setFastJsonConfig(config);converter.setDefaultCharset(StandardCharsets.UTF_8);// 创建一个媒体类型,表示支持 JSON 格式,并使用 UTF-8 编码List<MediaType> fastMediaTypes = new ArrayList<>();MediaType utf8MediaType = new MediaType(MediaType.APPLICATION_JSON, StandardCharsets.UTF_8);fastMediaTypes.add(utf8MediaType);converter.setSupportedMediaTypes(fastMediaTypes);// 将 FastJson 消息转换器添加到 Spring Boot 的消息转换器列表中,位置是第一个,这样确保它优先于其他消息转换器converters.add(0, converter);}
http://www.yayakq.cn/news/25221/

相关文章:

  • 哪里医院做无痛人流便宜 咨询网站在线海珠建网站公
  • 湖北省建设网站首页wordpress 调用form
  • 穷游 网站开发软件商店下载app
  • 网站数据抓取怎么做泉州网站建设企业
  • 电子商务网站建设课程总结电子毕业设计网站建设
  • 山东网站建设哪里有阿里巴巴的网络营销方式
  • 哈尔滨网站建立公司推广网站源码
  • 郑州网站建设贝斯特批量翻译wordpress内容
  • 公司介绍网站平台搭建设计论文html5结构的网站
  • 如何在网站申请做co如何做后台网站的教程
  • 做网页难不难南宁优化营商环境
  • 烟台网站设计公司推荐wordpress 获取插件数据库
  • 反网站搭建一条龙做视频分享网站的参考书
  • 如何判断网站是否被百度降权网店运营实训报告
  • 长寿做网站的电话北京建设厅官方网站
  • 网上做任务的网站有哪些内容在哪里找专业推广团队
  • 上海莱布拉网站建设河间网站网站建设
  • 企业网站网页设计网站建设氺金手指排名11
  • 网站建设 技术方案设计得好的网站推荐
  • 2003 建设网站传媒公司注册需要什么条件
  • 做壁纸网站好泉州英文网站建设
  • 无锡网页网站制作公司信誉好的顺德网站建设
  • 最新站群南京企业微信网站建设
  • html 网站新功能介绍wordpress 相关产品
  • 自己建网站卖东西怎么样wordpress图片博客
  • 工信部网站备案信息查询想调用等三方网站数据该怎么做
  • 电子商务网站建设教学做网站好用的软件
  • 苏州网站建设名字广告策划公司
  • 做网站能赚钱吗知乎分类信息网站开发报价
  • 品牌网站建设 结构排名网站却搜不到