這篇文章主要講解了“VO對(duì)象中byte[]數(shù)組怎么用JSON轉(zhuǎn)換”,文中的講解內(nèi)容簡(jiǎn)單清晰,易于學(xué)習(xí)與理解,下面請(qǐng)大家跟著小編的思路慢慢深入,一起來研究和學(xué)習(xí)“VO對(duì)象中byte[]數(shù)組怎么用JSON轉(zhuǎn)換”吧!
10年積累的成都做網(wǎng)站、網(wǎng)站設(shè)計(jì)經(jīng)驗(yàn),可以快速應(yīng)對(duì)客戶對(duì)網(wǎng)站的新想法和需求。提供各種問題對(duì)應(yīng)的解決方案。讓選擇我們的客戶得到更好、更有力的網(wǎng)絡(luò)服務(wù)。我雖然不認(rèn)識(shí)你,你也不認(rèn)識(shí)我。但先網(wǎng)站制作后付款的網(wǎng)站建設(shè)流程,更有濰城免費(fèi)網(wǎng)站建設(shè)讓你可以放心的選擇與我們合作。
如果VO對(duì)象中有byte[]數(shù)組如何處理?jason轉(zhuǎn)換的時(shí)候,給前端暴露出來的byte[]會(huì)轉(zhuǎn)換成String
但是前端發(fā)送的請(qǐng)求中,String轉(zhuǎn)換為byte[]可能會(huì)拋出異常:
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]
這個(gè)時(shí)候可以自定義序列化和反序列化方法來處理,具體代碼如下:
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; /** * 訂單狀態(tài) */ @ApiModelProperty("訂單狀態(tài)") private Integer status; /** * 商品code */ @ApiModelProperty(name = "商品Code") private String productCode; /** * 創(chuàng)建時(shí)間 */ @DateTimeFormat(pattern = "yyyy-MM-dd HH:mm:ss") @JsonFormat(pattern = "yyyy-MM-dd HH:mm:ss") @ApiModelProperty(name = "創(chuàng)建時(shí)間", 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轉(zhuǎn)換了。
感謝各位的閱讀,以上就是“VO對(duì)象中byte[]數(shù)組怎么用JSON轉(zhuǎn)換”的內(nèi)容了,經(jīng)過本文的學(xué)習(xí)后,相信大家對(duì)VO對(duì)象中byte[]數(shù)組怎么用JSON轉(zhuǎn)換這一問題有了更深刻的體會(huì),具體使用情況還需要大家實(shí)踐驗(yàn)證。這里是創(chuàng)新互聯(lián),小編將為大家推送更多相關(guān)知識(shí)點(diǎn)的文章,歡迎關(guān)注!