统一的返回格式重要性就不用我说了吧,凡是有经验的人都应该知道,
我们的数据有时候并不一定是丰满的,比如我有一个用户资料,账号密码肯定会有,但是昵称,性别什么的有时候却没有,将这类数据返回到前端的时候他们调用起来很有可能出现空指针异常,这种运行时异常需要在有特定数据的时候才会发现,如果每一次我们都效验一次的话会很麻烦,所以我们需要一个统一的视图
下面来实际操作一下,这里我们采用阿里巴巴的fastjson
首先,我们先引入依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.32</version>
</dependency>
然后创建一个配置文件,具体代码如下
package com.example.demo;
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.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
public class Config implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//新建一个解析试图
FastJsonHttpMessageConverter mFastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig FastJsonConfig = new FastJsonConfig();
FastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteNullNumberAsZero,
SerializerFeature.WriteNullStringAsEmpty
);
mFastJsonHttpMessageConverter.setFastJsonConfig(FastJsonConfig);
//将试图添加到列表内
converters.add(mFastJsonHttpMessageConverter);
}
}
@Configuration注解告诉springboot,这个文件是我们的配置文件
这里我们实现了WebMvcConfigurer接口,这个接口是springboot内部提供专门处理用户自行添加的配置,里面不仅仅包含了修改视图的过滤还有其他很多的方法,具体的以后再详细说明,
除了实现这个接口以外还可以继承自WebMvcConfigurationSupport 这个类
我们来看一下setSerializerFeatures这个方法的参数
这是一个枚举,里面有很多的东西,这里我们只了解一下常用的,其他的有兴趣大家可以自行了解
WriteMapNullValue:输出map的时候,如果value是空的,则不显示
WriteNullListAsEmpty:输出list的时候如果是null则输出长度为0的数组
WriteNullStringAsEmpty:输出string的时候,如果是null输出空字符串
WriteNullNumberAsZero:输出数值的时候,如果是null则输出0
WriteNullBooleanAsFalse:输出Boolean的时候,如实是null则输出false
我们来测试一下这个类啊,首先我们添加一个javabean
这里省略了get和set
我们在添加一个接口,
package com.example.demo;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class DemoA {
@RequestMapping(value="testfalstjson")
public Atest testfalstjson(){
return new Atest();
}
}
然后我们看一下执行效果
可以看到,我们返回的是newAtest,a和b都应该是null,但现在不是,说明我们这里配置成功了,
我们可以把之前那个类的内容注释了再试试
package com.example.demo;
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.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import java.util.List;
@Configuration
public class Config implements WebMvcConfigurer {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//新建一个解析试图
FastJsonHttpMessageConverter mFastJsonHttpMessageConverter = new FastJsonHttpMessageConverter();
FastJsonConfig FastJsonConfig = new FastJsonConfig();
FastJsonConfig.setSerializerFeatures(
// SerializerFeature.DisableCircularReferenceDetect,
// SerializerFeature.WriteNullNumberAsZero,
// SerializerFeature.WriteNullStringAsEmpty
);
mFastJsonHttpMessageConverter.setFastJsonConfig(FastJsonConfig);
//将试图添加到列表内
converters.add(mFastJsonHttpMessageConverter);
}
}
到这里的话就完全证明我们配置成功了,
当然,我们还可以自定义一个json试图,不过比较麻烦,这里我就不掩饰了
配套源码:https://gitee.com/ww2510095/springboot_learning/tree/master