0


Spring Boot中解析JSON数据的三种方案

文章目录

1 JSON简介

JSON(JavaScript Object Notation) 是一种轻量级的数据交换格式。易于人阅读和编写。同时也易于机器解析和生成。JSON是一个标记符的序列。这套标记符包含六个构造字符、字符串、数字和三个字面名。JSON是一个序列化的对象或数组。

2 Spring Boot默认的JSON解析

Spring Boot项目中当我们添加了spring-boot-starter-web依赖,就默认提供了Jackson用于解析Json

2.1 使用示例

创建一个实体类

@ComponentpublicclassUser{privatelong id;privateString username;privateDate birthday;publiclonggetId(){return id;}publicvoidsetId(long id){this.id = id;}publicStringgetUsername(){return username;}publicvoidsetUsername(String username){this.username = username;}publicDategetBirthday(){return birthday;}publicvoidsetBirthday(Date birthday){this.birthday = birthday;}}

创建一个Controller类,用于页面访问:

@RestControllerpublicclassShowController{@GetMapping("/show")publicList<User>show(){List<User> users=newArrayList<User>();for(int i =0; i <5; i++){User user=newUser();
      user.setId(i);
      user.setUsername("张三"+i);
      user.setBirthday(newDate());
      users.add(user);}return  users;}}

启动项目,浏览器访问,可以看到默认返回了JSON格式的数据
在这里插入图片描述
可以看到上面的Date类型数据需要我们自定义格式,在实体类中date属性上添加JsonFormat注解可以实现:

@JsonFormat(pattern ="YYYY-MM-dd")privateDate birthday;

修改后重启项目,浏览器访问:
在这里插入图片描述
但是当很多类都需要配置自定义格式时,就需要添加多个注解,这样是麻烦且不利于后期维护的。所以我们需要修改全局JSON格式。

下面以修改Date类型数据为例:

2.2 修改特定数据的全局JSON格式

JSON的解析离不开HttpMessageConvert,HttpMessageConvert是一个消息转换工具,主要有两方面的功能:

  1. 将服务端返回的对象序列化成JSON字符串
  2. 将前端传来的JSON字符串反序列化成Java对象

SpringMVC中默认配置了Jackson和GsonHttpMessageConverter,Spring Boot中对此做了自动化配置,主要由以下两个类实现:

org.springframework.boot.autoconfigure.http.JacksonHttpMessageConvertersConfigurationorg.springframework.boot.autoconfigure.http.GsonHttpMessageConvertersConfiguration

JacksonHttpMessageConvertersConfiguration的源码如下:

@Configuration(
  proxyBeanMethods =false)classJacksonHttpMessageConvertersConfiguration{JacksonHttpMessageConvertersConfiguration(){}@Configuration(
    proxyBeanMethods =false)@ConditionalOnClass({XmlMapper.class})@ConditionalOnBean({Jackson2ObjectMapperBuilder.class})protectedstaticclassMappingJackson2XmlHttpMessageConverterConfiguration{protectedMappingJackson2XmlHttpMessageConverterConfiguration(){}@Bean@ConditionalOnMissingBeanpublicMappingJackson2XmlHttpMessageConvertermappingJackson2XmlHttpMessageConverter(Jackson2ObjectMapperBuilder builder){returnnewMappingJackson2XmlHttpMessageConverter(builder.createXmlMapper(true).build());}}//配置类注解@Configuration(
    proxyBeanMethods =false)/*
  *下面几个是条件注解,当添加了Jackson依赖,就有了ObjectMapper.class和属性,后面的配置就会生效
  */@ConditionalOnClass({ObjectMapper.class})@ConditionalOnBean({ObjectMapper.class})@ConditionalOnProperty(
    name ={"spring.http.converters.preferred-json-mapper"},
    havingValue ="jackson",
    matchIfMissing =true)staticclassMappingJackson2HttpMessageConverterConfiguration{MappingJackson2HttpMessageConverterConfiguration(){}/*
*@ConditionalOnMissingBean的意思就是如果我们没有配置这个Bean,就自动创建一个默认的,当我们配置了就使用我们配置的Bean,不再创建
*/@Bean@ConditionalOnMissingBean(
      value ={MappingJackson2HttpMessageConverter.class},
      ignoredType ={"org.springframework.hateoas.server.mvc.TypeConstrainedMappingJackson2HttpMessageConverter","org.springframework.data.rest.webmvc.alps.AlpsJsonHttpMessageConverter"})/*
    *MappingJackson2HttpMessageConverter就是Jackson的消息转换工具类
    */MappingJackson2HttpMessageConvertermappingJackson2HttpMessageConverter(ObjectMapper objectMapper){/*
    *JSON中序列化对象主要用ObjectMapper工具类
    */returnnewMappingJackson2HttpMessageConverter(objectMapper);}}}

通过查看上面源码,可以知道我们修改生成的JSON的全局格式有以下两种方式:

2.2.1 自定义MappingJackson2HttpMessageConverter

自己创建一个MappingJackson2HttpMessageConverter,取代JacksonHttpMessageConvertersConfiguration中项目自带的。

例:
在上面创建的项目中,首先删除JsonFormat注解,下面新建一个配置类,内容如下:

@ConfigurationpublicclassWebMVCConfig{@BeanMappingJackson2HttpMessageConvertermappingJackson2HttpMessageConverter(){MappingJackson2HttpMessageConverter converter=newMappingJackson2HttpMessageConverter();ObjectMapper om=newObjectMapper();
    om.setDateFormat(newSimpleDateFormat("YYYY/MM/dd"));
    converter.setObjectMapper(om);return converter;}}
2.2.2 自定义ObjectMapper

通过上面的类可以看到类中主要起作用的是ObjectMapping,上面的JacksonHttpMessageConvertersConfiguration可以看ObjectMapping是直接注入的。它是在配置类JacksonAutoConfiguration 中提供的。
在这里插入图片描述
修改WebMVCConfig类,内容如下:

@ConfigurationpublicclassWebMVCConfig{@BeanObjectMapperobjectMapper(){ObjectMapper om=newObjectMapper();
    om.setDateFormat(newSimpleDateFormat("YYYY/MM/DD"));return om;}}

两种方式都是可以的,浏览器访问:
在这里插入图片描述

3 使用Gson处理JSON

3.1 使用示例

基于上面创建的项目进行修改,首先去掉spring-boot-starter-json依赖。然后添加gson依赖:
在这里插入图片描述
然后将配置类的jackson部分和一些导入的jackson包注释或删除,然后启动项目。
在这里插入图片描述
可以看到日期格式是Gson默认的。需要进行修改

3.2 修改特定数据的全局JSON格式

GsonHttpMessageConvertersConfiguration的部分源码如下:

@Configuration(
    proxyBeanMethods =false)@ConditionalOnBean({Gson.class})@Conditional({GsonHttpMessageConvertersConfiguration.PreferGsonOrJacksonAndJsonbUnavailableCondition.class})staticclassGsonHttpMessageConverterConfiguration{GsonHttpMessageConverterConfiguration(){}@Bean@ConditionalOnMissingBeanGsonHttpMessageConvertergsonHttpMessageConverter(Gson gson){GsonHttpMessageConverter converter =newGsonHttpMessageConverter();
      converter.setGson(gson);return converter;}

可以看到与Jackson差不多,所以可以仿照Jackson进行修改

3.2.1 自定义GsonHttpMessageConverter
@ConfigurationpublicclassWebMVCConfig{@BeanGsonHttpMessageConvertergsonHttpMessageConverter(){GsonHttpMessageConverter converter=newGsonHttpMessageConverter();
      converter.setGson(newGsonBuilder().setDateFormat("YYYY-MM-DD").create());return converter;}}
3.2.2 自定义Gson对象

上面配置主要用的是Gson对象,Gson对象是在GsonAutoConfiguration类中,当我们没有配置时为我们创建的。然后在GsonHttpMessageConverter中使用。我们也可以自己创建一个Gson对象:

@ConfigurationpublicclassWebMVCConfig{@BeanGsongson(){returnnewGsonBuilder().setDateFormat("YYYY-MM-DD").create();}}

两种方式都是可以的,浏览器访问:
在这里插入图片描述

4 使用FastJson处理JSON

4.1 使用示例

以上面创建的项目为基础,去除Jackson依赖,添加FastJson依赖:
在这里插入图片描述
因为Spring Boot没有提供相关的自动化配置类,所以我们需要手动创建FastJson的消息转换工具类:

@ConfigurationpublicclassWebMVCConfig{@BeanFastJsonHttpMessageConverterfastJsonHttpMessageConverter(){FastJsonHttpMessageConverter converter=newFastJsonHttpMessageConverter();return converter;}}

启动项目,浏览器访问:
在这里插入图片描述
可以看到日期格式变成了FastJson的格式了。需要进行修改。

4.2 修改特定数据的全局JSON格式

FastJson是直接在 FastJsonHttpMessageConverter类中进行修改,新建一个FastJsonConfig对象,此时需要对编码进行设置,否则会出现乱码:

@ConfigurationpublicclassWebMVCConfig{@BeanFastJsonHttpMessageConverterfastJsonHttpMessageConverter(){FastJsonHttpMessageConverter converter=newFastJsonHttpMessageConverter();FastJsonConfig config=newFastJsonConfig();// 处理中文乱码问题List<MediaType> fastMediaTypes =newArrayList<>();
    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);
    converter.setSupportedMediaTypes(fastMediaTypes);//设置日期格式
    config.setDateFormat("YYYY-MM-DD");
    converter.setFastJsonConfig(config);return converter;}}
标签: spring boot Java Json

本文转载自: https://blog.csdn.net/qq_43842093/article/details/134621819
版权归原作者 Archie_java 所有, 如有侵权,请联系我们删除。

“Spring Boot中解析JSON数据的三种方案”的评论:

还没有评论