SpringBoot 字节数组自动转化为Base64格式的问题

2022年6月4日13:38:59

SpringBoot 字节数组自动转化为Base64格式的问题

问题描述

Spring Boot微服务架构,两个服务间请求数据,发现byte数组格式的字段会自动转化为Base64格式

问题研究

经过研究发现,这个是因为RestTemplate在发起exchange的调用时候,会把字段类型为字节数组转化为Base64格式,然后再发起请求到目标服务。
代码位置:com.fasterxml.jackson.databind.ser.std.ByteArraySerializer这个类的serialize方法

publicvoidserialize(byte[] value,JsonGenerator g,SerializerProvider provider)throwsIOException{
        g.writeBinary(provider.getConfig().getBase64Variant(), value,0, value.length);}

依赖关系为:spring-boot-starter-web->spring-boot-starter-json->com.fasterxml.jackson.core->jackson-databind

结论

RestTemplate的exchange调用会把Request参数里面的字节数组转为Base64格式,这样做的原因可能有很多,我觉得有一点这样做的好处是可以节省空间,请参考如下
https://stackoverflow.com/questions/67156878/why-does-jackson-convert-byte-array-to-base64-string-on-converting-to-json

  • 作者:永远sayYES
  • 原文链接:https://blog.csdn.net/hudmhacker/article/details/120721720
    更新时间:2022年6月4日13:38:59 ,共 726 字。