【Spring Boot】025-返回 JSON 数据:常用的三种 JSON 转换器

【Spring Boot】025-返回 JSON 数据:常用的三种 JSON 转换器

一、第一种:默认的 jackson-databind

1、说明

默认情况下,类上使用 @Controller 注解,方法上使用 @ResponseBody 注解,返回的对象会默认被转换成 JSON 格式;

另外,@RestController 注解是 @Controller 注解 和 @ResponseBody 注解的组合,是等效的!

二、第二种:使用 Gson

1、Gson简介

GSON 是 Google 提供的用来在 Java 对象和 JSON 数据之间进行映射的 Java 类库。可以将一个JSON 字符转成一个 Java 对象,或者将一个 Java对象 转化为 JSON 字符串。

特点: 快速、高效;代码量少、简洁;面向对象;数据传递和解析方便

2、使用步骤

第一步:在 pom.xml 中移除默认的 jackson-databind ,引入 Gson 依赖

<!--web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!--gson-->
<dependency>
    <groupId>com.google.code.gson</groupId>
    <artifactId>gson</artifactId>
</dependency>

第二步:自定义 HttpMessageConverter

Spring Boot 已经默认提供了 Gson 的自动转换类 GsonHttpMessageConvertersConfigurations,因此 Gson 的以来添加之后,可以直接像使用 jackson-databind 那样直接使用 gson 。但是在 Gson 进行转换时,如果相对日期进行格式化,那么需要开发者自定义 HttpMessageConverter !

package com.zibo.api.config;

import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.json.GsonHttpMessageConverter;

import java.lang.reflect.Modifier;

@Configuration
public class GSONConfig {
    @Bean
    GsonHttpMessageConverter gsonHttpMessageConverter(){
        // 自己提供一个 GSONHttpMessageConverter 实例
        GsonHttpMessageConverter converter = new GsonHttpMessageConverter();
        GsonBuilder builder = new GsonBuilder();
        // 设置解析式日期的格式
        builder.setDateFormat("yyyy-MM-dd");
        // 设置解析时修饰符为 protected 的字段被过滤掉
        builder.excludeFieldsWithModifiers(Modifier.PROTECTED);
        // 创建 Gson 对象,放入 converter 实例并返回
        Gson gson = builder.create();
        converter.setGson(gson);
        return converter;
    }
}

三、第三种:使用 fastjson

1、简介

fastjson 是阿里巴巴的一个开源的 JSON 解析框架,是目前 JSON 解析速度最快的开源框架,该框架也可以集成到 Spring Boot 中,大但并不能立即使用!需要提供相应的 HttpMessageConverter 后才能使用!

2、使用步骤

第一步:在 pom.xml 中移除默认的 jackson-databind ,引入 fastjson 依赖

<!--web-->
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>com.fasterxml.jackson.core</groupId>
            <artifactId>jackson-databind</artifactId>
        </exclusion>
    </exclusions>
</dependency>

<!--fast json-->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.75</version>
</dependency>

第二步:自定义 HttpMessageConverter

package com.zibo.api.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.nio.charset.StandardCharsets;

@Configuration
public class MyFastJsonConfig {
    @Bean
    FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
        // 自己提供一个 FastJsonHttpMessageConverter 实例
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        // 设置解析式日期的格式
        config.setDateFormat("yyyy-MM-dd");
        // 设置解析时编码格式为 UTF-8
        config.setCharset(StandardCharsets.UTF_8);
        config.setSerializerFeatures(
                SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        );
        converter.setFastJsonConfig(config);
        return converter;
    }
}

3、另一种配置方式

package com.zibo.api.config;

import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

import java.nio.charset.StandardCharsets;
import java.util.List;

public class MyWebMvcConfig implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        // 自己提供一个 FastJsonHttpMessageConverter 实例
        FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
        FastJsonConfig config = new FastJsonConfig();
        // 设置解析式日期的格式
        config.setDateFormat("yyyy-MM-dd");
        // 设置解析时编码格式为 UTF-8
        config.setCharset(StandardCharsets.UTF_8);
        config.setSerializerFeatures(
                SerializerFeature.WriteClassName,
                SerializerFeature.WriteMapNullValue,
                SerializerFeature.PrettyFormat,
                SerializerFeature.WriteNullListAsEmpty,
                SerializerFeature.WriteNullStringAsEmpty
        );
        converter.setFastJsonConfig(config);
        converters.add(converter);
    }
}
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值