SpringBoot项⽬的JSON序列化输出
在⽬前web开发中,JSON是主流的数据传输⽅式,在SpringMVC中使⽤消息转换器HttpMessageConverter对JSON的转换提供了很好的⽀持,在SpringBAoot中,对JSON转换做了进⼀步的简化。常⽤的JSON序列化⽅式主要有三种:
A、默认⽅式
B、fastjson(alibaba)
C、Gson(google)
⾸先说⼀下默认Json序列G化的⽅式,这种⽅式只要在实体类的字段上添加注解即可。以下为案例:1、创建SpringBoot项⽬并在l⽂件中添加web依赖:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>com.hongke</groupId>
<artifactId>JsonApplication</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<description>this is json serialize demo</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</dependency>
</dependencies>
</project>
注:在web依赖中默认加⼊了jackson-databind作为JSON处理器,此时不在需要添加额外的JSON处理器。
2、创建实体类并添加序列化格式:
package ity;
import com.fasterxml.jackson.annotation.JsonFormat;
import com.fasterxml.jackson.annotation.JsonIgnore;
import lombok.AllArgsConstructor;
import lombok.Data;
import java.util.Date;
/**
* @author chengjunyu
* @classname Goods
* @description TODO
* @date 2020/7/25 21:54
*/
@Data
@AllArgsConstructor
public class Goods {
private String name;
private String property;
@JsonIgnore
//输出时忽略该字段
private Double price;
@JsonFormat(pattern = "yyyy-MM-dd")
//以yyyy-MM-dd的格式输出时间字段
private Date createTime;
}
3、创建Controller层接⼝请求:
package ller;
import ity.Goods;
import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController;
import java.util.Date;
/**
* @author chengjunyu
* @classname FoodController
* @description TODO
* @date 2020/7/25 21:56
*/
@RestController
@RequestMapping("/shop")
public class GoodsController {
@GetMapping("/getGoods")
public Goods getGoods() {
Goods goods = new Goods("蛋黄派", "⾯包", 18.5, new Date()); return goods;
}
}
4、创建启动类和application.properties⽂件:JsonApplication.java:
package com.hongke;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
/**
* @author chengjunyu
* @classname JsonApplication
* @description TODO
* @date 2020/7/25 21:52
*/
@SpringBootApplication
public class JsonApplication {
public static void main(String[] args) {
SpringApplication.run(JsonApplication.class);
}
}
applicaton.properties:
server.port=9003
5、Postman发送请求到localhost:9003/shop/getGoods,结果如下:
通过返回的结果,可以看出,价格字段未被展⽰,制造时间只显⽰了年-⽉-⽇,由此,默认格式的JSON序列化完成。
说完了默认⽅式实现JSON序列化后,接下来看看怎么样使⽤fastjson进⾏JSON序列化输出,fastjson 是alibaba开源的⼀个JSON解析框架,是⽬前JSON解析速度最快的开源框架。以下为案例:
1、修改默认JSON案例中的pom⽂件,内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="/POM/4.0.0"
xmlns:xsi="/2001/XMLSchema-instance"
xsi:schemaLocation="/POM/4.0.0 /xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion>
<groupId>com.hongke</groupId>
<artifactId>JsonApplication</artifactId>
<version>1.0</version>
<packaging>jar</packaging>
<description>this is json serialize demo</description>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
&porting.outputEncoding>UTF-8</porting.outputEncoding>
<java.version>1.8</java.version>
</properties>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.1.12.RELEASE</version>
</parent>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<exclusions>
<exclusion>
<groupId>com.</groupId>
<artifactId>jackson-databind:2.5.0</artifactId>
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.54</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
spring mvc和boot区别</dependency>
</dependencies>
</project>
注:去除web依赖中jackson-databind依赖,增加alibaba的fastjson依赖;
2、配置fastjson的消息转换器HttpMessageConverter,代码如下:MyFastJsonConfig.class
package fig;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fig.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import t.annotation.Bean;
import t.annotation.Configuration;
import java.nio.charset.Charset;
/**
* @author chengjunyu
* @classname MyFastJsonConfig
* @description TODO
* @date 2020/7/25 22:57
*/
@Configuration
public class MyFastJsonConfig {
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter() {
FastJsonHttpMessageConverter messageConverter = new FastJsonHttpMessageConverter(); FastJsonConfig fastJsonConfig = new FastJsonConfig();
//数据编码-UTF-8
fastJsonConfig.setCharset(Charset.forName("UTF-8"));
//⽇期格式
fastJsonConfig.setDateFormat("yyyy-MM-dd");
fastJsonConfig.setSerializerFeatures(
//在⽣成的JSON中输出类名
SerializerFeature.WriteClassName,
//输出value为null的数据
SerializerFeature.WriteMapNullValue,
//空集合输出[]⽽不是null
SerializerFeature.WriteNullListAsEmpty,
//空字符串输出""⽽不是null
SerializerFeature.WriteNullStringAsEmpty,
/
/⽣成的json格式话
SerializerFeature.PrettyFormat
);
messageConverter.setFastJsonConfig(fastJsonConfig);
return messageConverter;
}
}
3、修改实体类,代码如下:
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系QQ:729038198,我们将在24小时内删除。
发表评论