SpringBoot ResponseBody返回值处理的实现
作者:小安灬 时间:2023-06-29 23:18:43
1. SpringBoot ResponseBody 返回值中null值处理
@PostMapping(path = "/test", produces = MediaType.APPLICATION_JSON_VALUE)
public Object test() {
JSONObject jsonObject = new JSONObject();
jsonObject.put("test","test");
jsonObject.put("testnull",null);
ApiResponseVo apiResponseVo = new ApiResponseVo();
apiResponseVo.setData(jsonObject );
apiResponseVo.setStatus(0);
return apiResponseVo;
}
接口返回 (想实现将testnull也进行返回<null展示null还是0还是"" 可自定义>) :
{
"data": {
"test": "test"
},
"status": 0
}
import java.nio.charset.Charset;
import java.util.ArrayList;
import java.util.List;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
@Configuration
public class fastJsonConfig extends WebMvcConfigurationSupport {
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
FastJsonHttpMessageConverter converter = new FastJsonHttpMessageConverter();
FastJsonConfig config = new FastJsonConfig();
config.setSerializerFeatures(
// 保留 Map 空的字段
SerializerFeature.WriteMapNullValue,
// 将 String 类型的 null 转成""
// SerializerFeature.WriteNullStringAsEmpty,
// 将 Number 类型的 null 转成 0
// SerializerFeature.WriteNullNumberAsZero,
// 将 List 类型的 null 转成 []
// SerializerFeature.WriteNullListAsEmpty,
// 将 Boolean 类型的 null 转成 false
// SerializerFeature.WriteNullBooleanAsFalse,
// 避免循环引用
SerializerFeature.DisableCircularReferenceDetect
);
converter.setFastJsonConfig(config);
converter.setDefaultCharset(Charset.forName("UTF-8"));
List<MediaType> mediaTypeList = new ArrayList<>();
// 解决中文乱码问题,相当于在 Controller 上的 @RequestMapping 中加了个属性 produces = "application/json"
mediaTypeList.add(MediaType.APPLICATION_JSON);
converter.setSupportedMediaTypes(mediaTypeList);
converters.add(converter);
}
}
2. 拦截responsebody转json,对数据进行二次处理 (字典转换做案例)
2.1> 设置 *
import java.nio.charset.StandardCharsets;
import java.util.List;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.http.converter.StringHttpMessageConverter;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;
import com.alibaba.fastjson.serializer.SerializerFeature;
import com.alibaba.fastjson.support.config.FastJsonConfig;
import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import com.fintell.dp3.manager.interceptor.LogInterceptor;
@Configuration
public class WebMvcConfiguration implements WebMvcConfigurer {
@Override
public void addInterceptors(InterceptorRegistry registry) {
// 自定义 * ,添加拦截路径和排除拦截路径
registry.addInterceptor(getLogInterceptor()).addPathPatterns("/**");
}
@Bean
public LogInterceptor getLogInterceptor() {
return new LogInterceptor();
}
/**
* 修改StringHttpMessageConverter默认配置 & Json 默认序列化方式 (改这个方法)
*/
@Override
public void configureMessageConverters(List<HttpMessageConverter<?>> converters) {
//创建fastJson消息转换器
FastJsonHttpMessageConverter fastConverter = new FastJsonHttpMessageConverter();
//创建配置类
FastJsonConfig fastJsonConfig = new FastJsonConfig();
//修改配置返回内容的过滤
fastJsonConfig.setSerializerFeatures(
SerializerFeature.DisableCircularReferenceDetect,
SerializerFeature.WriteMapNullValue,
SerializerFeature.WriteNullStringAsEmpty
);
fastConverter.setFastJsonConfig(fastJsonConfig);
//将fastjson添加到视图消息转换器列表内
converters.add(fastConverter);
}
}
2.2> 字典注解类
import java.lang.annotation.ElementType;
import java.lang.annotation.Retention;
import java.lang.annotation.RetentionPolicy;
import java.lang.annotation.Target;
@Target({ElementType.FIELD})
@Retention(RetentionPolicy.RUNTIME)
public @interface Dict {
String type();
}
2.3> 序列化类
import java.io.IOException;
import java.lang.reflect.Field;
import org.apache.commons.lang3.StringUtils;
import org.springframework.context.annotation.Configuration;
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 lombok.extern.slf4j.Slf4j;
@Slf4j
@Configuration
public class DictJsonSerializer extends JsonSerializer<Object> {
@Override
public void serialize(Object dictVal, JsonGenerator generator, SerializerProvider provider) throws IOException {
ObjectMapper objectMapper = new ObjectMapper();
String currentName = generator.getOutputContext().getCurrentName();
try {
// 1> 获取字段
Field field = generator.getCurrentValue().getClass().getDeclaredField(currentName);
// 2> 获取字典注解
Dict dict = field.getDeclaredAnnotation(Dict.class);
// 3> 判断是否添加了字典注解
if(dict == null) {
objectMapper.writeValue(generator, dictVal);
return;
}
// 4> 获取注解的type值
String type = dict.type();
// **************** 以下依据实际业务处理即可 ********************
// 5> 获取到字段的值
String val = dictVal == null ? "" : dictVal.toString();
String dictValName = "";
if(!StringUtils.isEmpty(val)) {
// 6> 这里可以依据type做不同的处理逻辑
dictValName = "通过自己的方法,依据val获取到对应的字典值";
}
// 7> 将字段改写为{"code":"code","name":"name"}格式
objectMapper.writeValue(generator, BaseEnum.builder().code(dictVal).name(dictValName.toString()).build());
} catch (NoSuchFieldException e) {
log.error(e);
}
}
}
2.4> 字典注解使用
@SuppressWarnings("serial")
@Builder
@Getter
@Setter
@ToString
@NoArgsConstructor
@AllArgsConstructor
public class BizRuleDto implements Serializable{
// 指定使用哪种序列化
@JsonSerialize(using = DictJsonSerializer.class)
// 指定字典 (将被转换为{"code":"content","name":"contentname"}格式)
@Dict(type = "content")
private String content;
}
3. 其它补充 (从容器中获取某个实例) : 如 DictJsonSerializer 需要通过service查询数据库获取字典
@Slf4j
public class DictJsonSerializer extends JsonSerializer<Object> {
// service
private static ProductProxy productProxy;
static {
productProxy = SpringUtils.getBean(ProductProxy.class);
}
@Override
public void serialize(Object dictVal, JsonGenerator generator, SerializerProvider provider) throws IOException {
}
}
SpringUtils
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component;
@Component
public class SpringUtils implements ApplicationContextAware {
private static ApplicationContext ctx;
/**
* 获取bean
*/
@SuppressWarnings("unchecked")
public static <T> T getBean(String id) {
return (T) ctx.getBean(id);
}
/**
* 按类型获取bean
*/
public static <T> T getBean(Class<T> clazz) {
return ctx.getBean(clazz);
}
@Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
ctx = applicationContext;
}
}
来源:https://blog.csdn.net/qq_17522211/article/details/103886918
标签:SpringBoot,ResponseBody,返回值
0
投稿
猜你喜欢
springboot2.X整合prometheus监控的实例讲解
2021-07-04 13:49:59
java二维码生成的方法
2022-08-02 22:07:28
SpringCloud超详细讲解微服务网关Gateway
2021-09-11 14:54:49
Java内存模型JMM详解
2023-11-28 13:08:50
springboot mybatis里localdatetime序列化问题的解决
2023-06-25 06:58:18
Java 执行CMD命令或执行BAT批处理方式
2022-10-15 03:57:38
Android自定义图片集合
2022-06-24 11:34:52
C#添加Windows服务 定时任务
2021-10-07 15:12:22
java插入排序 Insert sort实例
2023-07-21 07:07:37
快速理解Java垃圾回收和jvm中的stw
2021-09-06 20:27:17
C#在DataTable中根据条件删除某一行的实现方法
2022-03-09 05:34:19
详解JAVA动态代理
2023-11-24 22:52:04
利用Spring Session和redis对Session进行共享详解
2021-10-18 18:22:45
Android中实现Webview顶部带进度条的方法
2022-04-22 15:45:43
Springmvc返回html页面问题如何解决
2021-07-05 09:29:45
优化常见的java排序算法
2022-03-25 05:11:54
C#读取文件MD5值的实现代码
2021-10-08 18:01:39
Android 自定义来电秀实现总结
2023-12-17 07:52:32
Unity实战之FlyPin(见缝插针)小游戏的实现
2022-05-21 19:46:31
java抓包后对pcap文件解析示例
2022-11-23 20:21:53