实际开发中,我们一定遇到过这样的问题:前端显示和后台存储数据单位不统一,而且各有各自的理由,统一不了,那就转换吧。

每次返回给前端时再转换一遍,返回给前端的json数据,在后端里定义的往往是一个对象,如何做到优雅的转换呢?只需两步

1. 写一个负责转换的类,里面写好规则
 public class MySerializerUtils extends JsonSerializer<Integer> {
@Override
public void serialize(Integer status, JsonGenerator jsonGenerator, SerializerProvider serializerProvider) throws IOException, JsonProcessingException {
String statusStr = "";
switch (status) {
case 0:
statusStr = "暂存";
break;
case 1:
statusStr = "待上报";
break;
case 2:
statusStr = "待审核";
break;
case 3:
statusStr = "已审";
break;
case 4:
statusStr = "退回";
break;
case -1:
statusStr = "已删";
break;
default:
statusStr = "状态信息不符合";
}
jsonGenerator.writeString(statusStr);
}
}
2. 在实体类上需要装换的字段上加上注解
/**
* 文字型状态值
*/
@JsonSerialize(using = MySerializerUtils.class)
private int status;

这样就转换成功了。

@JsonSerialize注解,主要用于数据转换,该注解作用在该属性的getter()方法上。

												

@JsonSerialize的使用的更多相关文章

  1. Hibernate JPA中@Transient、@JsonIgnoreProperties、@JsonIgnore、@JsonFormat、@JsonSerialize等注解解释

    @jsonignore的作用作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响. http://www.cnblogs.com/toSeeMyDream/p/443 ...

  2. springboot @RequestBody使用JsonSerialize与 JsonDeserialize自定义转参数,处理Date日期格式

    JsonDeserialize: 1.请求接收的是一个json,并且由对象接收 @RequestMapping(value = "/query", method = {Reques ...

  3. @JsonSerialize @JsonIgnoreProperties @JsonIgnore @JsonFormat

    @JsonIgnoreProperties 此注解是类注解,作用是json序列化时将java bean中的一些属性忽略掉,序列化和反序列化都受影响. @JsonIgnore 此注解用于属性或者方法上( ...

  4. Jaskson精讲第6篇-自定义JsonSerialize与Deserialize实现数据类型转换

    Jackson是Spring Boot(SpringBoot)默认的JSON数据处理框架,但是其并不依赖于任何的Spring 库.有的小伙伴以为Jackson只能在Spring框架内使用,其实不是的, ...

  5. @JsonFormat、@DateTimeFormat、@JsonSerialize注解的使用

    @JsonFormat 是jackson的注解,用于后台返回前台的时候将后台的date类型数据转为string类型格式化显示在前台,加在get方法或者date属性上面,因为 @JsonFormat 注 ...

  6. Spring+Mybatis基于注解整合Redis

    基于这段时间折腾redis遇到了各种问题,想着整理一下.本文主要介绍基于Spring+Mybatis以注解的形式整合Redis.废话少说,进入正题. 首先准备Redis,我下的是Windows版,下载 ...

  7. AFNetworking 与 gbk 编码格式后台数据的使用

    仅针,后台数据为GBK编码时的AFNetWorking 使用情况: 1. Request failed: unacceptable content-type: text/html     soluti ...

  8. 在C#中实现Json的序列化与反序列化

    第一种方式利用 JavaScriptSerializer [对应的Assembly 为 System.Web.Extensions.dll] 进行处理: public static class Kas ...

  9. XML序列化和反序列化

    上篇总结了下JSON的序列化和反序列化,博园中大牛给了很多牛叉的评论,学习了不少. 不过在上篇中忘了把json序列化和反序列化的另外一种方式写上去了,这里做个简单的补充: Json篇:http://w ...

随机推荐

  1. 泡泡一分钟:Semantic Labeling of Indoor Environments from 3D RGB Maps

    张宁 Semantic Labeling of Indoor Environments from 3D RGB Maps Manuel Brucker,  Maximilian Durner,  Ra ...

  2. Codeforces 570E - Pig and Palindromes - [滚动优化DP]

    题目链接:https://codeforces.com/problemset/problem/570/E 题意: 给出 $n \times m$ 的网格,每一格上有一个小写字母,现在从 $(1,1)$ ...

  3. linux终端提示符显示bash-4.2#

    原因是root在/root下面的几个配置文件丢失,丢失文件如下: 1..bash_profile 2..bashrc 以上这些文件是每个用户都必备的文件. 使用以下命令从主默认文件重新拷贝一份配置信息 ...

  4. Node项目的Restful化

    提倡Restful风格的目的或者作用主要是,结构清晰.符合标准.易于理解.扩展方便. 个人把Restful简单粗暴地理解为:路由不包含动词. 怎么做到路由不包含动词呢?答案是,启用常用的GET和POS ...

  5. Windows10远程桌面Ubuntu16.04

    一.Ubuntu16.04端软件安装(管理员权限) 1.安装xrdp sudo apt-get install xrdp 2.安装vnc4server sudo apt-get install vnc ...

  6. npm install的时候出现unexpected end of file错误提示时的解决办法

    运行npm cache clean --force 清空npm缓存即可决这个问题 参考:https://blog.csdn.net/jingtian678/article/details/811876 ...

  7. MAVEN_day02快速入门

    一.MAVEN工程目录结构 二.怎么在Eclipse中集成M2E插件(工欲善其事必先利其器)有一些准备工作 1.修改自己下载的MAVEN 2.设置本地仓库 三.构建MAVEN工程 1.选择“MAVEN ...

  8. HDU 2586 How far away(dfs+邻接表)

    How far away [题目链接]How far away [题目类型]dfs+邻接表 &题意: 题目大意:一个村子里有n个房子,这n个房子用n-1条路连接起来,接下了有m次询问,每次询问 ...

  9. CentOS 7 配置Tomcat9连接MySQL

    配置Tomcat 首先安装Tomcat 安装Tomcat分为安装Tomcat和安装JDK两个步骤 JDK( Java Development Kit ) 是Sun Microsystems针对Java ...

  10. mongodb 安装遇到问题:the domain,user name and/or password are incorrect.remember to use"." for the domain if the account is on the local machine

    安装mongoDB遇到如下问题:the domain,user name  and/or password are incorrect.remember to use"." for ...