How to use the postgresql bytea with jersey and hibernate?

时间:2019-06-01 14:12:31

标签: arrays json postgresql hibernate jersey

I am currently working with a REST server with Java especially Jersey and Hibernate for persistence. I also have a Postgresql database.

I own a table "pictures" with the following structure :

  • number: integer (PK)
  • register: date
  • picture: bytea
  • mime: varchar
  • owner_id: integer (FK)

My java class is:

public class Picture  implements Serializable {
  @JsonProperty("numero")
  private Long numero;

  @JsonProperty("register")
  @JsonFormat(pattern = "dd.MM.yyyy")
  private LocalDate register;

  @JsonProperty("picture")
  private byte[] picture;

  @JsonProperty("mime")
  private String mime;

  @JsonIgnore
  private Owner owner;


  ...

My mapping orm:

<?xml version="1.0" encoding="UTF-8" ?>
<entity-mappings>
    <package>ch.hearc.aspoba.model</package>
    <entity class="Picture">
        <table name="pictures"/>
        <attributes>
            <id name="numero">
                <column name="numero"/>
                <generated-value strategy="IDENTITY"/>
            </id>
            <basic name="register">
                <column name="register" nullable="true"/>
            </basic>
            <basic name="mime">
                <column name="mime" nullable="false"/>
            </basic>
            <many-to-one name="owner">
                <join-column name="owner_id"/>
            </many-to-one>
        </attributes>
    </entity>
</entity-mappings>

My persistence class:

    public Set<Picture> getPictures(Long ownerId){
         return inTransaction(() ->{
             Owner owner = getEntityManager().find(Owner.class, ownerId);
             List<Picture> pictures = owner.getPictures();
             return new HashSet<>(pictures);
        });
    }

To test, I directly added an image in my database using DataGrip (Button add "load file").

Then, I make a select: select picture from pictures Result:

STUwNGU0NzBkMGExYTBhMDAwMDAwMGQ0OTQ4NDQ1MjAwMDAwMDA1MDAwMDAwMDUwODA2MDAwMDAwOGQ2ZjI2ZTUwMDAwMDAxYzQ5NDQ0MTU0MDhkNzYzZjhmZmZmM2ZjMzdmMDYyMDA1YzMyMDEyODRkMDMxZjE4MjU4Y2QwNDAwMGVmNTM1Y2JkMThlMGUxZjAwMDAwMDAwNDk0NTRlNDRhZTQyNjA4Mg==

When I make my request with postman, I get a table of bytes but it is not the same as with the select.

KzU0NTU3NzRlNDc1NTMwNGU3YTQyNmI0ZDQ3NDU3ODU5NTQ0MjY4NGQ0NDQxNzc0ZDQ0NDE3NzRkNDc1MTMwNGY1NDUxMzQ0ZTQ0NTEzMTRkNmE0MTc3NGQ0NDQxNzc0ZDQ0NDEzMTRkNDQ0MTc3NGQ0NDQxNzc0ZDQ0NTU3NzRmNDQ0MTMyNGQ0NDQxNzc0ZDQ0NDE3NzRmNDc1MTMyNWE2YTQ5MzI1YTU0NTU3NzRkNDQ0MTc3NGQ0NDQxNzg1OTdhNTEzNTRlNDQ1MTMwNGQ1NDU1MzA0ZDQ0Njg2YjRlN2E1OTdhNWE2YTY4NmQ1YTZkNWE2ZDRkMzI1YTZhNGQ3YTY0NmQ0ZDQ0NTk3OTRkNDQ0MTMxNTk3YTRkNzk0ZDQ0NDU3OTRmNDQ1MjZiNGQ0NDRkNzg1YTZhNDUzNDRkNmE1NTM0NTkzMjUxNzc0ZTQ0NDE3NzRkNDc1NjZkNGU1NDRkMzE1OTMyNGE2YjRkNTQ2ODZjNGQ0NzU1Nzg1YTZhNDE3NzRkNDQ0MTc3NGQ0NDQxNzc0ZTQ0NmIzMDRlNTQ1MjZjNGU0NDUyNjg1YTU0NTE3OTRlNmE0MTM0NGQ2NzNkM2Q=

Moreover when I test my results with a utility like: https://base64.guru/converter/decode/image. They are in "text / plain" instead of "image / png".

My question is: How to correctly manage the bytea so jersey sends me a usable array of bytes?

0 个答案:

没有答案