前言:   数据库的字段比如: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;@Overridepublic 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;@Overridepublic void setApplicationContext(ApplicationContext applicationContext) throws BeansException {if(SpringUtil.applicationContext == null) {SpringUtil.applicationContext = applicationContext;}System.out.println("========ApplicationContext配置成功,在普通类可以通过调用SpringUtils.getAppContext()获取applicationContext对象,applicationContext="+SpringUtil.applicationContext+"========");}//获取applicationContextpublic 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返回指定的Beanpublic static <T> T getBean(String name,Class<T> clazz){return getApplicationContext().getBean(name, clazz);}}


转载于:https://www.cnblogs.com/liran123/p/9945385.html

FastJson序列化Json自定义返回字段,普通类从spring容器中获取bean相关推荐

  1. 在普通类中获取spring容器中的bean

    在普通类中获取spring容器中的bean 1.工具类 package com.itheima.hchat.util;import org.springframework.beans.BeansExc ...

  2. SpringBoot 之 普通类获取Spring容器中的bean

    SpringBoot 之 普通类获取Spring容器中的bean 转载于:https://www.cnblogs.com/lwmp/p/8892927.html

  3. 【SpringBoot】在普通类中获取spring容器中的bean

    这段时间公司搞封闭开发,做一个联通总部的客服系统项目,是基于springboot的.在开发工程中遇到一个页面datagrid数据排序的功能,因为有多个表的数据都要用到排序功能,于是我就写了一个排序功能 ...

  4. Spring Boot中普通类获取Spring容器中的Bean

    我们知道如果我们要在一个类使用spring提供的bean对象,我们需要把这个类注入到spring容器中,交给spring容器进行管理,但是在实际当中,我们往往会碰到在一个普通的Java类中,自己动手n ...

  5. 从spring容器中获取对象工具类

    工具类: public class SpringConfigTool implements ApplicationContextAware {private static ApplicationCon ...

  6. 获取Spring容器管理的Bean工具类

    很多时候我们在一些不受spring管理的类中需要用到spring管理的Bean,那么这个时候可以使用如下工具类从spring容器中获取相关的Bean实例. @Component public clas ...

  7. JPA 自定义返回字段

    实体类:User.java @Data @Accessors(chain = true) @EqualsAndHashCode(callSuper = true) @Entity @Table(nam ...

  8. Spring的工具类,方便在非spring管理环境中获取bean

    场景 在SpringBoot的后台项目中,如果想要引入并且调用某个bean,可以直接通过注解的方式. 比如在单元测试中引入某业务的Controller @RunWith(SpringJUnit4Cla ...

  9. 工具类:获取 spring 容器中 bean

    前些天发现了一个巨牛的人工智能学习网站,通俗易懂,风趣幽默,忍不住分享一下给大家.点击跳转到教程. package com.orange.utils;import org.springframewor ...

最新文章

  1. linux启停was命令,linux下的启停脚本
  2. 正式开课!如何学习相机模型与标定?(单目+双目+鱼眼+深度相机)
  3. 数学建模感悟:新的算法的学习
  4. 自学linux指令分析-find
  5. mysql or优化_MySQL 语句优化
  6. Java GregorianCalendar getMaximum()方法与示例
  7. Linux 第20天: (09月12日) Linux启动和内核管理
  8. 关于碰撞检测和物理引擎
  9. 轻而易举地激发变革:开放的方法
  10. java类的封装关系_Java—类的封装、继承与多态
  11. 华擎计算机主板配置,DDR+478+PCIEx16当下最平民化的计算机配置
  12. 全球与中国医疗AR VR市场深度研究分析报告
  13. 【进阶】QQ聊天机器人--群聊机器人篇
  14. org.springframework.amqp.AmqpConnectException: java.net.ConnectException: Connection refused: connec
  15. Jquery datatable 动态隐藏列(根据有无值)
  16. LINUX学习-在LCD上显示多行文字
  17. 西门子200SMART(十)写程序的思路
  18. C语言制作-QQ聊天室
  19. 后勤管理系统—服务台管理功能
  20. 通过小程序和微信社群来构建产品运营体系

热门文章

  1. Android应用打开外部文件
  2. webpack2--webpack 4.X 快速创建demo
  3. 教程:使用Data Lake Analytics + OSS分析CSV格式的TPC-H数据集
  4. 使用Kotlin打造Android路由框架-KRouter
  5. crontab 命令
  6. JS正则表达式验证身份证号码
  7. IOS反汇编工具Hopper分析Crash Log
  8. Apache - AH00558
  9. JavaScript Math和Number对象
  10. 利用XSL对XML数据进行加密和大小写转换