Greetings There is a need to transfer an array of bytes by json. But I can't do it. The project is written in Java using Spring-Hibenate-JPA.

The essence of News in which I declare the image_url field, Postgres base, the field in the database is of type BYTEA.

 @Column(name = "image_url", nullable = false, length = 10000) private byte[] image_url; 

I add to the database array "b":

 String url = "http://78.24.216.172:8080/images/" + nameImage + ".jpeg"; byte[] b = url.getBytes(); 

Then I just request all the entries in this way:

 @Query("select distinct p from News p ORDER BY p.id DESC") List<News> findNewsNew(); 

But for some reason in image_url is not an array of bytes but just a string, how can I solve this problem?

enter image description here

  • 2
    Read about base64 and the problem will disappear. - Vladimir Martyanov
  • Vladimir, this is understandable, but I just need an array of bytes. - Android EE

2 answers 2

I solved the problem by replacing the library for Json, I used Jackson before, now I switched to Gson, and the byte array is displayed correctly.

    Try converting the string to an byte array.

      public byte[] stringToHex(String string) { return new BigInteger(string, 16).toByteArray(); }