小工具知识网

当前位置: 首页 > 行业知识 > Spring Boot中解析JSON数据的三种方案

行业知识

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

2026-11-04 15:15:26 来源:互联网转载

文章目录

      • 1 JSON简介
      • 2 Spring Boot默认的JSON解析
        • 2.1 使用示例
        • 2.2 修改特定数据的全局JSON格式
          • 2.2.1 自定义MappingJackson2HttpMessageConverter
          • 2.2.2 自定义ObjectMapper
          • 3 使用Gson处理JSON
            • 3.1 使用示例
            • 3.2 修改特定数据的全局JSON格式
              • 3.2.1 自定义GsonHttpMessageConverter
              • 3.2.2 自定义Gson对象
              • 4 使用FastJson处理JSON
                • 4.1 使用示例
                • 4.2 修改特定数据的全局JSON格式

                  1 JSON简介

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

                  2 Spring Boot默认的JSON解析

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

                  2.1 使用示例

                  创建一个实体类

                  @Componentpublic class User {  private long id;  private String username;  private Date birthday;  public long getId() {    return id;  }  public void setId(long id) {    this.id = id;  }  public String getUsername() {    return username;  }  public void setUsername(String username) {    this.username = username;  }  public Date getBirthday() {    return birthday;  }  public void setBirthday(Date birthday) {    this.birthday = birthday;  }}

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

                  @RestControllerpublic class ShowController {  @GetMapping("/show")  public List show(){    List users=new ArrayList();    for (int i = 0; i  

                  启动项目,浏览器访问,可以看到默认返回了JSON格式的数据

                  可以看到上面的Date类型数据需要我们自定义格式,在实体类中date属性上添加JsonFormat注解可以实现:

                    @JsonFormat(pattern = "YYYY-MM-dd")  private Date 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)class JacksonHttpMessageConvertersConfiguration {  JacksonHttpMessageConvertersConfiguration() {  }  @Configuration(    proxyBeanMethods = false  )  @ConditionalOnClass({XmlMapper.class})  @ConditionalOnBean({Jackson2ObjectMapperBuilder.class})  protected static class MappingJackson2XmlHttpMessageConverterConfiguration {    protected MappingJackson2XmlHttpMessageConverterConfiguration() {    }    @Bean    @ConditionalOnMissingBean    public MappingJackson2XmlHttpMessageConverter mappingJackson2XmlHttpMessageConverter(Jackson2ObjectMapperBuilder builder) {      return new MappingJackson2XmlHttpMessageConverter(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  )  static class MappingJackson2HttpMessageConverterConfiguration {    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的消息转换工具类    */    MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(ObjectMapper objectMapper) {    /*    *JSON中序列化对象主要用ObjectMapper工具类    */      return new MappingJackson2HttpMessageConverter(objectMapper);    }  }}

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

                  2.2.1 自定义MappingJackson2HttpMessageConverter

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

                  例:

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

                  @Configurationpublic class WebMVCConfig {  @Bean  MappingJackson2HttpMessageConverter mappingJackson2HttpMessageConverter(){    MappingJackson2HttpMessageConverter converter=new MappingJackson2HttpMessageConverter();    ObjectMapper om=new ObjectMapper();    om.setDateFormat(new SimpleDateFormat("YYYY/MM/dd"));    converter.setObjectMapper(om);    return converter;  }}
                  2.2.2 自定义ObjectMapper

                  通过上面的类可以看到类中主要起作用的是ObjectMapping,上面的JacksonHttpMessageConvertersConfiguration可以看ObjectMapping是直接注入的。它是在配置类JacksonAutoConfiguration 中提供的。

                  修改WebMVCConfig类,内容如下:

                  @Configurationpublic class WebMVCConfig {  @Bean  ObjectMapper objectMapper(){    ObjectMapper om=new ObjectMapper();    om.setDateFormat(new SimpleDateFormat("YYYY/MM/DD"));    return om;  }}

                  两种方式都是可以的,浏览器访问:

                  3 使用Gson处理JSON

                  3.1 使用示例

                  基于上面创建的项目进行修改,首先去掉spring-boot-starter-json依赖。然后添加gson依赖:

                  可以看到日期格式是Gson默认的。需要进行修改

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

                  GsonHttpMessageConvertersConfiguration的部分源码如下:

                    @Configuration(    proxyBeanMethods = false  )  @ConditionalOnBean({Gson.class})  @Conditional({GsonHttpMessageConvertersConfiguration.PreferGsonOrJacksonAndJsonbUnavailableCondition.class})  static class GsonHttpMessageConverterConfiguration {    GsonHttpMessageConverterConfiguration() {    }    @Bean    @ConditionalOnMissingBean    GsonHttpMessageConverter gsonHttpMessageConverter(Gson gson) {      GsonHttpMessageConverter converter = new GsonHttpMessageConverter();      converter.setGson(gson);      return converter;    }

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

                  3.2.1 自定义GsonHttpMessageConverter
                  @Configurationpublic class WebMVCConfig {    @Bean      GsonHttpMessageConverter gsonHttpMessageConverter(){      GsonHttpMessageConverter converter=new GsonHttpMessageConverter();      converter.setGson(new GsonBuilder().setDateFormat("YYYY-MM-DD").create());      return converter;       } }
                  3.2.2 自定义Gson对象

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

                  @Configurationpublic class WebMVCConfig {       @Bean  Gson gson(){   return new GsonBuilder().setDateFormat("YYYY-MM-DD").create();       }}

                  两种方式都是可以的,浏览器访问:

                  4 使用FastJson处理JSON

                  4.1 使用示例

                  因为Spring Boot没有提供相关的自动化配置类,所以我们需要手动创建FastJson的消息转换工具类:

                  @Configurationpublic class WebMVCConfig {  @Bean  FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){    FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter();    return converter;  }  }

                  启动项目,浏览器访问:

                  可以看到日期格式变成了FastJson的格式了。需要进行修改。

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

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

                  @Configurationpublic class WebMVCConfig {  @Bean  FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){    FastJsonHttpMessageConverter converter=new FastJsonHttpMessageConverter();    FastJsonConfig config=new FastJsonConfig();    // 处理中文乱码问题    List fastMediaTypes = new ArrayList();    fastMediaTypes.add(MediaType.APPLICATION_JSON_UTF8);    converter.setSupportedMediaTypes(fastMediaTypes);    //设置日期格式    config.setDateFormat("YYYY-MM-DD");    converter.setFastJsonConfig(config);    return converter;  }} 

上一篇:英语单词怎么写汉语?

下一篇:服务器待机怎么唤醒?