一、前情提要
上一次我们做完了使用Cros
解决跨域问题,这一此我们做一个前后端交换信息所需要的东西——JSON,及解决JSON值为空的时候不出现NULL。
二、任务详情
- 创建自定义JSON工具类
- 配置JSON使JSON值为空的时候不出现NULL
三、相关介绍
1.什么是JSON?
在《维基百科——JSON》是这样说的:
JSON(JavaScript Object Notation,JavaScript对象表示法)是一种由道格拉斯·克罗克福特构想和设计、轻量级的数据交换语言,该语言以易于让人阅读的文字为基础,用来传输由属性值或者序列性的值组成的数据对象。尽管JSON是JavaScript的一个子集,但JSON是独立于语言的文本格式,并且采用了类似于C语言家族的一些习惯。
JSON 数据格式与语言无关,脱胎于 JavaScript,但当前很多编程语言都支持 JSON 格式数据的生成和解析。JSON 的官方 MIME 类型是 application/json,文件扩展名是 .json。
JSON其实不算是一种语言,更合适的说法是一种储存信息的结构格式,它的结构是以键值对的形式来表示。
下面的图片为XML和JSON的对比(图片源于网络,侵权请联系我,立即删除)
四、实践操作
1.创建自定义JSON工具类
由于我们在通过接口返回JSON数据时,总需要有一定格式,不然每个接口的JSON格式都不一致,那前端请求后,要给每个不同风格的JSON数据,那样就会显得很麻烦,所以自定义JSON很有必要。
自定义JSON的三大要素:
- 响应业务状态码
status
(通过不同的状态码判断请求是否成功) - 响应的消息
msg
(通过消息提示前端请求时出现什么问题) - 响应的数据
data
(将必要的数据通过键值对的形式显示)
在com.repairsystem.utils
创建工具类JsonResult
package com.repairsystem.utils;
/**
* @author CheungChingYin
* @date 2019/1/8
* @time 14:26
* @Description: 自定义响应数据结构
* 门户接受此类数据后需要使用本类的方法转换成对于的数据类型格式(类,或者list)
* 其他自行处理
* 200:表示成功
* 500:表示错误,错误信息在msg字段中
* 501:bean验证错误,不管多少个错误都以map形式返回
* 502:拦截器拦截到用户token出错
* 555:异常抛出信息
*/
public class JsonResult {
//响应业务状态
private Integer status;
//响应消息
private String msg;
//响应中的数据
private Object data;
public JsonResult(){
}
public JsonResult(Object data) {
this.status = 200;
this.msg = "OK";
this.data = data;
}
public JsonResult(Integer status, String msg, Object data) {
this.status = status;
this.msg = msg;
this.data = data;
}
public JsonResult build(Integer status, String msg, Object data) {
return new JsonResult(status, msg, data);
}
public static JsonResult ok(){
return new JsonResult(null);
}
public static JsonResult ok(Object data){
return new JsonResult(data);
}
public static JsonResult errorMsg(String msg){
return new JsonResult(500,msg,null);
}
public static JsonResult errorMap(Object data){
return new JsonResult(501,"BeanError",data);
}
public static JsonResult errorTokenMsg(String msg){
return new JsonResult(502,msg,null);
}
public static JsonResult errorException(String msg){
return new JsonResult(555,msg,null);
}
public Integer getStatus() {
return status;
}
public void setStatus(Integer status) {
this.status = status;
}
public String getMsg() {
return msg;
}
public void setMsg(String msg) {
this.msg = msg;
}
public Object getData() {
return data;
}
public void setData(Object data) {
this.data = data;
}
}
2.创建JSON配置类JacksonConfig
在com.repairsystem.config
下创建JSON配置类JacksonConfig
package com.repairsystem.config;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializerProvider;
import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;
import java.io.IOException;
/**
* 配置Jackson值为null的时候为空
* @author CheungChingYin
* @date 2019/1/8
* @time 14:41
*/
@Configuration
public class JacksonConfig {
@Bean
@Primary
@ConditionalOnMissingBean(ObjectMapper.class)
public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
ObjectMapper objectMapper = builder.createXmlMapper(false).build();
objectMapper.getSerializerProvider().setNullValueSerializer(new JsonSerializer<Object>() {
@Override
public void serialize(Object o, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException {
jsonGenerator.writeString("");
}
});
return objectMapper;
}
}
3.测试问题是否解决
(1)编写接口InterfaceTest
测试需要有一个接口,这里先提前写一写接口。
注意:测试完成后记得删除!
在com.repairsystem.web.controller
创建接口InterfaceTest
里面注解@RestController
相当于@ResponseBody
+ @Controller
package com.repairsystem.web.controller;
import com.repairsystem.utils.JsonResult;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;
import java.util.HashMap;
/**
* @author CheungChingYin
* @date 2019/1/8
* @time 14:48
*/
@RestController
public class InterfaceTest {
@GetMapping("/InterfaceTest")
public JsonResult test1(){
HashMap<String,String> map = new HashMap<String,String>();
map.put("Test1","1234");
map.put("name","CheungChingYin");
map.put("email",null);
return JsonResult.ok(map);
}
}
(2)运行项目
项目运行后,访问http://localhost:8081/InterfaceTest
会看到如下结果(由于我的浏览器有JSONView
,所以显示有结构,如果没有插件是看到一行没有结构的JSON)
发现我写故意留空的地方没有显示null
,而是使用""
代替,基本解决JSON的值空时不出现Null的问题。
到这里,配置自定义JSON
类及解决JSON
的值空时不出现Null已经完成了。如果您对次篇文章有疑问,可以在文章下方留言,谢谢您的阅读。如对【机房报修管理系统】系列文章有兴趣,可以关注或收藏我的文章,您的支持是我最大的动力,我会尽快推出下一期内容,敬请期待。