VO对象中byte[]数组怎么用JSON转换
发表于:2024-09-24 作者:千家信息网编辑
千家信息网最后更新 2024年09月24日,这篇文章主要讲解了"VO对象中byte[]数组怎么用JSON转换",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"VO对象中byte[]数组怎么用JSON
千家信息网最后更新 2024年09月24日VO对象中byte[]数组怎么用JSON转换
这篇文章主要讲解了"VO对象中byte[]数组怎么用JSON转换",文中的讲解内容简单清晰,易于学习与理解,下面请大家跟着小编的思路慢慢深入,一起来研究和学习"VO对象中byte[]数组怎么用JSON转换"吧!
如果VO对象中有byte[]数组如何处理?jason转换的时候,给前端暴露出来的byte[]会转换成String
但是前端发送的请求中,String转换为byte[]可能会抛出异常:
com.fasterxml.jackson.core.JsonParseException: Failed to decode VALUE_STRING as base64 (MIME-NO-LINEFEEDS): Illegal character '"' (code 0x22) in base64 content at [Source: java.io.PushbackInputStream@1d21ffcf; line: 4, column: 20]
这个时候可以自定义序列化和反序列化方法来处理,具体代码如下:
VO类:OrderVO
import java.util.Date; import org.springframework.format.annotation.DateTimeFormat; import com.fasterxml.jackson.annotation.JsonFormat; import com.fasterxml.jackson.databind.annotation.JsonDeserialize; import com.fasterxml.jackson.databind.annotation.JsonSerialize; import io.swagger.annotations.ApiModel; import io.swagger.annotations.ApiModelProperty; import lombok.Data; /** * * OrderVO. 订单模型 * */ @Data @ApiModel("订单模型") public class OrderVO { /** * 订单ID */ @ApiModelProperty("订单ID") private Long Id; /** * 订单Code */ @ApiModelProperty("订单Code") private String code; /** * 订单名称 */ @ApiModelProperty("订单名称") private String name; /** * 订单状态 */ @ApiModelProperty("订单状态") private Integer status; /** * 商品code */ @ApiModelProperty(name = "商品Code") private String productCode; /** * 创建时间 */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(name = "创建时间", readOnly = true) private Date createdDate; /** * 用户名称 */ @ApiModelProperty("用户名称") private String username; /** * remark */ @ApiModelProperty("remark") private String remark; /** * other, just a demo field */ @ApiModelProperty("other") @JsonSerialize(using = MyByteSerialize.class) @JsonDeserialize(using = MyByteDeSerializer.class) private byte[] other; }
反序列化类:MyByteDeSerializer
import java.io.IOException; import org.apache.commons.lang.StringUtils; import com.fasterxml.jackson.core.JsonParser; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.DeserializationContext; import com.fasterxml.jackson.databind.JsonDeserializer; public class MyByteDeSerializer extends JsonDeserializer{ @Override public byte[] deserialize(JsonParser p, DeserializationContext ctxt) throws IOException, JsonProcessingException { String stringValue = p.getText(); if (StringUtils.isEmpty(stringValue)) { return null; } return stringValue.getBytes(); } }
序列化类:MyByteSerialize
import java.io.IOException; import java.nio.charset.Charset; import com.fasterxml.jackson.core.JsonGenerator; import com.fasterxml.jackson.core.JsonProcessingException; import com.fasterxml.jackson.databind.JsonSerializer; import com.fasterxml.jackson.databind.SerializerProvider; public class MyByteSerialize extends JsonSerializer{ @Override public void serialize(byte[] value, JsonGenerator gen, SerializerProvider serializers) throws IOException, JsonProcessingException { if (null == value || 0 == value.length) { gen.writeString(""); } gen.writeString(new String(value, Charset.defaultCharset())); } }
这样就可以方法的处理byte[]类型的json转换了。
感谢各位的阅读,以上就是"VO对象中byte[]数组怎么用JSON转换"的内容了,经过本文的学习后,相信大家对VO对象中byte[]数组怎么用JSON转换这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!
订单
对象
数组
名称
序列
学习
内容
前端
商品
方法
时候
时间
模型
状态
用户
处理
代码
就是
思路
情况
数据库的安全要保护哪些东西
数据库安全各自的含义是什么
生产安全数据库录入
数据库的安全性及管理
数据库安全策略包含哪些
海淀数据库安全审计系统
建立农村房屋安全信息数据库
易用的数据库客户端支持安全管理
连接数据库失败ssl安全错误
数据库的锁怎样保障安全
微信小程序数据库恢复
齐齐哈尔市网络安全周活动
会员功能在数据库如何实现
隐藏数据库发展方法
谈谈维护网络安全的积极意义
数据库删除后还能创建吗
网络安全实践题
查看数据库使用的端口号
如何清理服务器共享文件夹
紫晶系统数据库密码
嵌入式软件开发 有双休吗
假期安全小贴士网络安全控制上网
电信弹窗广告服务器
怎样在家学习软件开发
云南省网络安全
ncbi数据库官网
深圳网络安全产业现状
数据库加密技术结果分析
数据库系统运行环境
计算机网络技术学校要分数吗
我的世界服务器删掉皮
聊城市网络安全教程
怎么打武装突袭3服务器
33岁不想做软件开发
保定网络技术推荐厂家
中国没有根域名服务器
威胁数据库安全的因素
齐齐哈尔商城分销软件开发
船闸升船机网络安全整改
我国网络安全实行什么保护