千家信息网

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转换这一问题有了更深刻的体会,具体使用情况还需要大家实践验证。这里是,小编将为大家推送更多相关知识点的文章,欢迎关注!

0