从零搭建自己的SpringBoot后台框架(四)

古城微笑少年丶 2023-08-17 15:29 171阅读 0赞

一:消息转换器能干什么?

不知道大家有没有遇到过这种情况:后台接口返回一个实例,当你需要使用某个属性的值时,你还要判断一下值是否为null;接口返回一堆属性值为null的属性等

ok,消息转换器可以帮你解决这个问题

二:添加fastjson依赖

打开pom.xml,找到<dependencies></dependencies>标签,在标签中添加fastjson依赖

  1. <dependency>
  2. <groupId>com.alibaba</groupId>
  3. <artifactId>fastjson</artifactId>
  4. <version>1.2.22</version>
  5. </dependency>

然后鼠标右键选择Maven→Reimport进行依赖下载

三:创建配置文件

在文件夹configurer中创建WebConfigurer

  1. package com.example.demo.core.configurer;
  2. import com.alibaba.fastjson.serializer.SerializerFeature;
  3. import com.alibaba.fastjson.support.config.FastJsonConfig;
  4. import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter4;
  5. import org.springframework.context.annotation.Configuration;
  6. import org.springframework.http.MediaType;
  7. import org.springframework.http.converter.HttpMessageConverter;
  8. import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
  9. import java.nio.charset.Charset;
  10. import java.util.ArrayList;
  11. import java.util.List;
  12. /**
  13. * @author phubing
  14. * @Description:
  15. * @time 2019/4/19 10:42
  16. */
  17. @Configuration
  18. public class WebConfigurer extends WebMvcConfigurationSupport {
  19. /**
  20. * 修改自定义消息转换器
  21. * @param converters
  22. */
  23. @Override
  24. public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
  25. FastJsonHttpMessageConverter4 converter = new FastJsonHttpMessageConverter4();
  26. converter.setSupportedMediaTypes(getSupportedMediaTypes());
  27. FastJsonConfig config = new FastJsonConfig();
  28. config.setSerializerFeatures(
  29. // String null -> ""
  30. SerializerFeature.WriteNullStringAsEmpty,
  31. // Number null -> 0
  32. SerializerFeature.WriteNullNumberAsZero,
  33. //禁止循环引用
  34. SerializerFeature.DisableCircularReferenceDetect
  35. );
  36. converter.setFastJsonConfig(config);
  37. converter.setDefaultCharset(Charset.forName("UTF-8"));
  38. converters.add(converter);
  39. }
  40. private List<MediaType> getSupportedMediaTypes() {
  41. List<MediaType> supportedMediaTypes = new ArrayList<>();
  42. supportedMediaTypes.add(MediaType.APPLICATION_JSON);
  43. supportedMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
  44. supportedMediaTypes.add(MediaType.APPLICATION_ATOM_XML);
  45. supportedMediaTypes.add(MediaType.APPLICATION_FORM_URLENCODED);
  46. supportedMediaTypes.add(MediaType.APPLICATION_OCTET_STREAM);
  47. supportedMediaTypes.add(MediaType.APPLICATION_PDF);
  48. supportedMediaTypes.add(MediaType.APPLICATION_RSS_XML);
  49. supportedMediaTypes.add(MediaType.APPLICATION_XHTML_XML);
  50. supportedMediaTypes.add(MediaType.APPLICATION_XML);
  51. supportedMediaTypes.add(MediaType.IMAGE_GIF);
  52. supportedMediaTypes.add(MediaType.IMAGE_JPEG);
  53. supportedMediaTypes.add(MediaType.IMAGE_PNG);
  54. supportedMediaTypes.add(MediaType.TEXT_EVENT_STREAM);
  55. supportedMediaTypes.add(MediaType.TEXT_HTML);
  56. supportedMediaTypes.add(MediaType.TEXT_MARKDOWN);
  57. supportedMediaTypes.add(MediaType.TEXT_PLAIN);
  58. supportedMediaTypes.add(MediaType.TEXT_XML);
  59. return supportedMediaTypes;
  60. }
  61. }

其中

  1. config.setSerializerFeatures()

方法中可以添加多个配置,以下列举出几个常用配置,更多配置请自行百度

  1. WriteNullListAsEmpty List字段如果为null,输出为[],而非null
  2. WriteNullStringAsEmpty 字符类型字段如果为null,输出为"",而非null
  3. DisableCircularReferenceDetect :消除对同一对象循环引用的问题,默认为false(如果不配置有可能会进入死循环)
  4. WriteNullBooleanAsFalseBoolean字段如果为null,输出为false,而非null
  5. WriteMapNullValue:是否输出值为null的字段,默认为false复制代码

四:数据库中添加测试数据

  1. INSERT INTO `user_info` VALUES ('1', '1');
  2. INSERT INTO `user_info` VALUES ('2', null);复制代码

五:测试

查询条件id为2

未配置转换器时,查询结果为

  1. {
  2. "code": 200,
  3. "msg": "success",
  4. "data": {
  5. "id": 2,
  6. "userName": null
  7. }
  8. }

配置转换器之后,查询结果为

  1. {
  2. "code": 200,
  3. "data": {
  4. "id": 2,
  5. "userName": "" //这里已经变为"",而不是null
  6. },
  7. "msg": "success"
  8. }

#

发表评论

表情:
评论列表 (有 0 条评论,171人围观)

还没有评论,来说两句吧...

相关阅读