Reputation: 209
I am developing an Android App with a Java Backend using Spring Data JPA. One of my classes represents a profile image and contains the following field:
@Lob
@Column(name = "image")
private byte[] image;
When i invoke the Rest API that saves the above class the JSON request looks like this:
{"image":"[B@23bafb3"}
Then the described error occurs. I have seen suggestions of declaring the image as string and then get the bytes but this does not seem a good practice to me.
Upvotes: 0
Views: 4638
Reputation: 296
the issue is with the serialization of the byte[]
to JSON. The default serialization of a byte[]
in Java will result in the string representation you have shown which is not a valid format for a binary image.
To correctly serialize the byte[]
as a base64 encoded string, you can use a custom serializer/deserializer in your API. One option is to use the Base64
class in Java to encode the byte[]
as a string and then decode the string back to a byte[]
during deserialization.
Upvotes: 1