FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean
前言:
数据库的字段比如:price:1 ,返回需要price:1元。
这时两种途径修改:
① 比如sql中修改或者是在实体类转json前遍历修改。
②返回json,序列化时候修改。用到的是fastjson。要求fastjson版本1.2.15以上(本章介绍)
操作:
首先pom修改依赖
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>1.2.29</version>
</dependency>
这里我要返回的图片路径 在返回时候字段前面拼接一个 /files
@JSONField(serializeUsing = JSONFieldViewPathSerializerUtil.class)
private String idcardImages;
JSONFieldViewPathSerializerUtil 是此字段序列化要用的类
public class JSONFieldViewPathSerializerUtil implements ObjectSerializer {
static StorageProperties storageProperties;
@Override
public void write(JSONSerializer serializer, Object object, Object fieldName, Type fieldType, int features) {
// 这里从spring中获取bean 为了取 "/files" ,可以忽略不看
if (null == storageProperties) {
storageProperties = SpringUtil.getBean(StorageProperties.class);
}
//主要看这里
if (StringUtils.isNotBlank(object.toString())) {
String value = object.toString();
//拼接 "/files"
value = storageProperties.getRequestLocation() + value;
serializer.write(newValue);
} else {
serializer.write(String.valueOf(object));
}
}
}
注意事项:
1、String序列化时候 如果是null 会在返回的时候变成 " "空字符串,判断时候需要注意
2、在要序列化的字符串是空 " " 也要 执行方法:serializer.write(String.valueOf(object));
否则返回的json 是“ idcardImages: ,” 注意格式是错误的
扩展:SpringUtil从容器中获取bean
/**
* @Auther liran
* @Date 2018/8/30 14:49
* @Description
*/
import org.springframework.beans.BeansException;
import org.springframework.context.ApplicationContext;
import org.springframework.context.ApplicationContextAware;
import org.springframework.stereotype.Component; @Component
public class SpringUtil implements ApplicationContextAware { private static ApplicationContext applicationContext; @Override
public void setApplicationContext(ApplicationContext applicationContext) throws BeansException {
if(SpringUtil.applicationContext == null) {
SpringUtil.applicationContext = applicationContext;
}
System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="+SpringUtil.applicationContext+"========");
} //获取applicationContext
public static ApplicationContext getApplicationContext() {
return applicationContext;
} //通过name获取 Bean.
public static Object getBean(String name){
return getApplicationContext().getBean(name);
} //通过class获取Bean.
public static <T> T getBean(Class<T> clazz){
return getApplicationContext().getBean(clazz);
} //通过name,以及Clazz返回指定的Bean
public static <T> T getBean(String name,Class<T> clazz){
return getApplicationContext().getBean(name, clazz);
} }
FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean的更多相关文章
- SpringBoot 之 普通类获取Spring容器中的bean
[十]SpringBoot 之 普通类获取Spring容器中的bean 我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器 ...
- [十]SpringBoot 之 普通类获取Spring容器中的bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,想直接使用 ...
- Spring Boot中普通类获取Spring容器中的Bean
我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...
- SpringBoot之普通类获取Spring容器中的bean
package com.geostar.geostack.git_branch_manager.common; import org.springframework.beans.BeansExcept ...
- 如何在自定义Listener(监听器)中使用Spring容器管理的bean
正好以前项目中碰到这个问题,现在网上偶然又看到这个问题的博文,那就转一下吧. 原文:http://blog.lifw.org/post/46428852 感谢作者 另外补充下:在web Server容 ...
- springBoot中怎么减少if---else,怎么动态手动注册类进入Spring容器
由于业务中经常有需要判断的if--eles操作,层层嵌套,看起来程序的可读性太差,结合策略模式进行改造 方法一.一般有策略模式 + 工厂模式进行代码的优化,减少 if---else: 方法二.还有 ...
- 普通java类加入spring容器的四种方式
今天在自己开发的工具类中使用了spring注入的方式调用了其他类,但是发生的报错,在整理了后今天小结一下. 首先简单介绍下spring容器,spring容器是整个spring框架的核心,通常我们说的s ...
- 2.spring源码-BeanPostProcessor后置处理之ApplicationContextAwareProcessor,实现spring容器中某一个类的bean对象在初始化时需要得到Spring容器内容。
需求:我们的需求是,在spring初始化完毕时,使我们自定义一个类Bird类可以得到spring容器内容. 实现步骤: 1.首先我们来看一下ApplicationContextAwareProcess ...
- 获取Spring容器中Bean实例的工具类(Java泛型方法实现)
在使用Spring做IoC容器的时候,有的类不方便直接注入bean,需要手动获得一个类型的bean. 因此,实现一个获得bean实例的工具类,就很有必要. 以前,写了一个根据bean的名称和类型获取b ...
随机推荐
- UltraISO制作CentOS 7.6 U盘引导安装盘
一.制作准备: 1.UltraISO下载安装 2.CentOS镜像文件下载(阿里镜像下载) 二.制作引导盘: 1.电脑插入U盘 2.UltraISO加载镜像文件: 文件->打开->选择对应 ...
- MySQL--MHA与GTID
##==========================================## MySQL 5.6版本引入GTID来解决主从切换时BINLOG位置点难定位的问题,MHA从0.56版本开始 ...
- jquery mobile Touch事件
Touch事件在用户触摸屏幕(页面)时触发 1.jquery mobile tap tap事件在用户敲击某个元素时触发 $("p").on("tap",fucn ...
- 腾讯技术分享:微信小程序音视频与WebRTC互通的技术思路和实践
1.概述 本文来自腾讯视频云终端技术总监rexchang(常青)技术分享,内容分别介绍了微信小程序视音视频和WebRTC的技术特征.差异等,并针对两者的技术差异分享和总结了微信小程序视音视频和WebR ...
- LeetCode题解33.Search in Rotated Sorted Array
33. Search in Rotated Sorted Array Suppose an array sorted in ascending order is rotated at some piv ...
- Python爬虫1-使用urlopen
GitHub代码练习地址:https://github.com/Neo-ML/PythonPractice/blob/master/SpiderPrac01_urlopen.py 爬虫简介- 爬虫定义 ...
- Numpy学习三:数组运算
1.转置 #reshape(shape)函数改变数组形状,shape是一个元组,表示数组的形状 创建一个包含15个元素的一维数组,通过reshape函数调整数组形状为3行5列的二维数组arr = np ...
- pycharm注册码(不断更新)
2019.3.13测试可用 MTW881U3Z5-eyJsaWNlbnNlSWQiOiJNVFc4ODFVM1o1IiwibGljZW5zZWVOYW1lIjoiTnNzIEltIiwiYXNzaWd ...
- ubuntu ImageMagick 图像转换工具
ImageMagick(简称 IM)是一个支持 GPL 协议的开源免费软件包.它由一组命令行工具组成的.它可以对超过 100 种的图像格式(包括 DPX, EXR, GIF, JPEG, JPEG-2 ...
- 安卓TabLayout+ViewPager实现切页
安卓使用TabLayout+ViewPager+Fragment 实现页面切换,可实现左右滑动切换视图界面和点击切换 可自定义菜单栏是在顶部还是在底部 一.实现效果: 二.实现过程: 2.1 一些重要 ...