0

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.

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

1 Comment

As per @Yacine, you can't put a byte[] in JSON because to contains non-readable characters. it is standard practice to Base64 encode (to a readable character only) String so that it can be serialised to JSON.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.