SpringBoot学习笔记:Json数据的处理
Posted 听风者-better
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了SpringBoot学习笔记:Json数据的处理相关的知识,希望对你有一定的参考价值。
1.SpringBoot默认对Json的处理
Springboot默认使用jackson对数据进行处理,我们先看下默认的处理方式
1.1 创建User实体类
public class User
private int id;
private String username;
private String password;
private Date birthday;
//省略set/get和带参构造方法
1.2 创建Controller类
@RestController
@RequestMapping(value = "/user")
public class UserController
@GetMapping(value = "/one")
public User getUser()
User user = new User(1, "唐万言", "123456", new Date());
return user;
@GetMapping(value = "/list")
public List<User> getUserList()
User u1 = new User(1, "唐万言", "123456", new Date());
User u2 = new User(2, "王心心", null, new Date());
List<User> list = new ArrayList<>();
list.add(u1);
list.add(u2);
return list;
1.3 测试返回结果
请求:http://localhost:8080/user/one
"id": 1,
"username": "唐万言",
"password": "123456",
"birthday": "2020-05-10T15:06:18.767+0000"
请求:http://localhost:8080/user/list
[
"id": 1,
"username": "唐万言",
"password": "123456",
"birthday": "2020-05-10T15:06:47.825+0000"
,
"id": 2,
"username": "王心心",
"password": null,
"birthday": "2020-05-10T15:06:47.825+0000"
]
1.4 自定义json的格式化配置
从返回值可以看出返回值都可以转成相应的 json 格式,但是格式都是默认,我们可以自己写一个配置类修改返回的格式
@Configuration
public class JsonConfig
@Bean
ObjectMapper objectMapper()
ObjectMapper objectMapper = new ObjectMapper();
//格式化日期
objectMapper.setDateFormat(new SimpleDateFormat("yyyy/MM/dd"));
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>()
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException
jsonGenerator.writeString("");
);
return objectMapper;
返回json格式
[
"id": 1,
"username": "唐万言",
"password": "123456",
"birthday": "2020/05/10"
,
"id": 2,
"username": "王心心",
"password": "",
"birthday": "2020/05/10"
]
2.使用FastJson的设置
2.1 引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.35</version>
</dependency>
2.2 添加配置
fastjson需要添加配置,不然json还是用默认的jackson处理
@Configuration
public class JsonConfig
@Bean
FastJsonHttpMessageConverter fastJsonHttpMessageConverter()
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
converter.setFastJsonConfig(config);
config.setDateFormat("yyyy/MM/dd");
config.setSerializerFeatures(SerializerFeature.WriteNullStringAsEmpty);
converter.setDefaultCharset(Charset.forName("UTF-8"));
//解决中文乱码问题
List<MediaType> mediaTypes = new ArrayList<>();
mediaTypes.add(MediaType.APPLICATION_JSON);
converter.setSupportedMediaTypes(mediaTypes);
return converter;
3.同一返回Json格式数据
3.1 创建统一json格式类
public class BaseResult<T>
private String code;
private String message;
private T data;
/**
* 无数据返回,操作成功
*/
public BaseResult()
this.code = "0";
this.message = "操作成功";
/**
* 没有数据返回,自定义状态码和提示信息
*
* @param code
* @param message
*/
public BaseResult(String code, String message)
this.code = code;
this.message = message;
/**
* 有数据返回时,操作成功
*
* @param data
*/
public BaseResult(T data)
this.code = "0";
this.message = "操作成功";
this.data = data;
/**
* 有数据返回时,状态码为0,自定义提示信息
*
* @param message
* @param data
*/
public BaseResult(T data, String message)
this.code = "0";
this.message = message;
this.data = data;
//省略set/get方法
3.2 修改Controller返回结果
@RestController
@RequestMapping(value = "/user")
public class UserController
@GetMapping(value = "/one")
public BaseResult<User> getUser()
User user = new User(1, "唐万言", "123456", new Date());
return new BaseResult<>(user);
@GetMapping(value = "/list")
public BaseResult<List> getUserList()
User u1 = new User(1, "唐万言", "123456", new Date());
User u2 = new User(2, "王心心", null, new Date());
List<User> list = new ArrayList<>();
list.add(u1);
list.add(u2);
return new BaseResult<>(list,"数据集获取成功");
3.3 请求返回结果
请求:http://localhost:8080/user/one
"code": "0",
"data":
"birthday": "2020/05/10",
"id": 1,
"password": "123456",
"username": "唐万言"
,
"message": "操作成功"
请求:http://localhost:8080/user/list
"code": "0",
"data": [
"birthday": "2020/05/10",
"id": 1,
"password": "123456",
"username": "唐万言"
,
"birthday": "2020/05/10",
"id": 2,
"password": "",
"username": "王心心"
],
"message": "数据集获取成功"
以上是关于SpringBoot学习笔记:Json数据的处理的主要内容,如果未能解决你的问题,请参考以下文章