Network Security Internet Technology Development Database Servers Mobile Phone Android Software Apple Software Computer Software News IT Information

In addition to Weibo, there is also WeChat

Please pay attention

WeChat public account

Shulou

How to use JSON to convert byte [] array in VO object

2025-04-11 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >

Share

Shulou(Shulou.com)06/02 Report--

This article mainly explains the "VO object byte [] array how to use JSON conversion", the article explains the content is simple and clear, easy to learn and understand, now please follow the editor's ideas slowly in-depth, together to study and learn "VO object byte [] array how to use JSON conversion" it!

What if there is a byte [] array in the VO object? When jason is converted, the byte [] exposed to the front end will be converted to String.

However, in the request sent by the front end, converting String to byte [] may throw an exception:

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]

At this point, you can customize the serialization and deserialization methods, as shown in the following code:

VO class: 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. Order Model * / @ Data @ ApiModel ("order Model") public class OrderVO {/ * order ID * / @ ApiModelProperty ("order ID") private Long Id / * * order Code * / @ ApiModelProperty ("order Code") private String code; / * order name * / @ ApiModelProperty ("order name") private String name / * * order status * / @ ApiModelProperty ("order status") private Integer status; / * Commodity code * / @ ApiModelProperty (name = "Commodity Code") private String productCode / * creation time * / @ DateTimeFormat (pattern = "yyyy-MM-dd HH:mm:ss") @ JsonFormat (pattern = "yyyy-MM-dd HH:mm:ss") @ ApiModelProperty (name = "creation time", readOnly = true) private Date createdDate / * * user name * / @ ApiModelProperty ("user name") 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;}

Deserialization class: 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 ();}}

Serialization class: 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 ());}}

This allows you to handle json conversions of type byte [] in a way.

Thank you for reading, the above is the "VO object byte [] array how to use JSON conversion" content, after the study of this article, I believe you on the VO object byte [] array how to use JSON conversion of this problem has a deeper understanding, the specific use of the need for you to practice verification. Here is, the editor will push for you more related knowledge points of the article, welcome to follow!

Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.

Views: 273

*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.

Share To

Internet Technology

Wechat

© 2024 shulou.com SLNews company. All rights reserved.

12
Report