springboot与json的邂逅

目录

一、序列化与反序列化

二、json三大主流框架

2.1springboot整合Jackson

2.2springboot整合gson

2.3springboot整合fastjson 


一、序列化与反序列化

序列化:对象--->json 常见场景:接口响应数据

反序列化:json--->对象 常见场景:接口请求参数

二、json三大主流框架

Jackson,gson,fastjson

Jackson与gson在springboot中已经集成好了,当我们引入spring-boot-start-web依赖后即可使用,而在使用gson或fastjson要使用的话需要排除spring-boot-start-web依赖中json依赖在额外引入gson或fastjson依赖.

2.1springboot整合Jackson

springboot中已经集成好了jackson,在JacksonAutoConfiguration中可以看到已经有了ObjectMapper

但是我们可以自己注册有个定制化的ObjectMapper,例如返回结果中对对象序列化时改变属性格式

@Configuration
public class Config {
    SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
    @Bean
    public ObjectMapper getObjectMapper(){
        ObjectMapper om = new ObjectMapper();
        om.setDateFormat(dateFormat);
        return om;
    }
}
@RestController
public class TestController {
    @GetMapping("/test")
    public Person test(){
        Person person = new Person();
        person.setId("1");
        person.setName("motionlesstar");
        person.setAge("99");
        person.setBirtyday(new Date());
        return person;
    }
}

 返回结果:

"id": "1",

"name": "motionlesstar",

"age": "99",

"birtyday": "2023-12-04 22:58:56"

   ###将java对象转换为JSON字符串

2.2springboot整合gson

用法Jackson类似,但要先排除Jackson依赖

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-json</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.google.code.gson</groupId>
            <artifactId>gson</artifactId>
        </dependency>
@Configuration
public class Config {
@Bean
    public GsonBuilder getGson(){
        GsonBuilder gsonBuilder = new GsonBuilder();
        gsonBuilder.setDateFormat("yyyy-MM-dd HH:mm");//区别jackson
        return gsonBuilder;
    }
}

 {

"id": "1",

"name": "motionlesstar",

"age": "99",

"birtyday": "2023-12-04 23:18"

}

2.3springboot整合fastjson 

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
            <exclusions>
                <exclusion>
                    <artifactId>spring-boot-starter-json</artifactId>
                    <groupId>org.springframework.boot</groupId>
                </exclusion>
            </exclusions>
        </dependency>
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>2.0.32</version>
        </dependency>

这里注意啊,fastjson你需要给一个版本号,因为springboot框架中并没有定义它的版本

然而与Jackson和gson不同的是,添加依赖后我们并不能直接使用它,直接使用会报错,

DefaultHandlerExceptionResolver : Resolved [org.springframework.http.converter.HttpMessageNotWritableException: No converter found for return value of type: class com.motionlesstar.domain.Person]

说我们没有提供一个converter,这个converter是一个转换器,我们的出参入参的序列化就是它来转换的,这三个json框架都实现了springMVC的HttpMessageConverter<T>接口,只不过jackson与gson已经提供了conver,所以这俩加上依赖就能用,但是fastjson需要我们自己提供一个converter.

@Configuration
public class Config {
    @Bean
   public FastJsonHttpMessageConverter fastJsonHttpMessageConverter(){
       FastJsonHttpMessageConverter converter  = new FastJsonHttpMessageConverter();
       FastJsonConfig fastJsonConfig = new FastJsonConfig();
       fastJsonConfig.setCharset(Charset.forName("UTF-8"));
       fastJsonConfig.setDateFormat("yyyy-MM-dd HH:mm:ss");
       converter.setFastJsonConfig(fastJsonConfig);
       return converter;
    }
}

返回结果: 

{

"age": "99",

"birtyday": "2023-12-04 23:56:06",

"id": "1",

"name": "motionlesstar"

}

 也可以在WebMvcConfigurer 中添加FastJsonHttpMessageConverter 

@Configuration
public class Config implements WebMvcConfigurer {
    @Override
    public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
        FastJsonHttpMessageConverter converter  = new FastJsonHttpMessageConverter();
        FastJsonConfig fastJsonConfig = new FastJsonConfig();
        fastJsonConfig.setCharset(Charset.forName("UTF-8"));
        fastJsonConfig.setDateFormat("yyyy-MM-dd HH");
        converter.setFastJsonConfig(fastJsonConfig);
        converters.add(converter);
    }

}

{

"age": "99",

"birtyday": "2023-12-05 00",

"id": "1",

"name": "motionlesstar"

最后,虽然fastjson号称解析最快,可是bug也多,在我写demo的时候,开始使用的是<version>2.0.32</version>版本,但是时间添加时间解析的时候解析不出来,这样

{

"age": "99",

"birtyday": 1701705827926,

"id": "1",

"name": "motionlesstar"

}

所以各位道友请酌情考虑哦. 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值