1

I am sending an array from my Angular frontend to Spring Boot backend and this array is persisted as a bytea in the postgresql db, but when i retrieve it via a request from the frontend to the backend I get a string of characters.

I would be highly obliged if someone could shed some light on this.

What is being sent to the backend:

{
     "image": [137,80,78,71,13,10,26,1,0,0,0,13,73,72,68,82,0,0,2,0,............,0,0,1,83,6,0,0,79,138,180,62,0,0,0,4,115,66,73,84,8,8,124,8,100,136,0,0,0,95,122,84,88,116,82,97,119,32,112,114]
}
What I receive from the backend:
{ "image": "iVBORw0KGgoAAAANSUhEUgAAAg..............RCKRSCQSiUQikUgkEolEIpFIJBKJRCKRSCTywsD8DaQezLUeSCTybCb+gUQikW8Yp6"
}

My Entity class containing the image field is:

package com.adh.inventory.entity;

import java.sql.Types;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.Lob;
import javax.persistence.Table;
import javax.validation.constraints.Min;
import javax.validation.constraints.NotNull;

import org.hibernate.annotations.GenericGenerator;
import org.hibernate.annotations.Type;

@Entity
@Table(name="ItemsTable")
public class ItemEntity {

    @Id
    @GenericGenerator(name="itemIdGen", strategy="com.adh.inventory.entity.ItemIdGenerator")
    @GeneratedValue(generator="itemIdGen")
    private String itemId;

    private String itemType;
    
    @Min(value=0)
    private Double quantity;
    
    private Double costPrice;
    
    @Min(value=0)
    private Double sellingPrice;
    
    @NotNull
    private String color;
    
    private String material;
    
    private String store;
    
    private String details;
    
    private byte[] image;
    
    private String trader;
    
    private String status;
    
    public String getItemId() {
        return itemId;
    }
    public void setItemId(String itemId) {
        this.itemId = itemId;
    }
    public String getItemType() {
        return itemType;
    }
    public void setItemType(String itemType) {
        this.itemType = itemType;
    }
    public Double getQuantity() {
        return quantity;
    }
    public void setQuantity(Double quantity) {
        this.quantity = quantity;
    }
    public Double getCostPrice() {
        return costPrice;
    }
    public void setCostPrice(Double costPrice) {
        this.costPrice = costPrice;
    }
    public Double getSellingPrice() {
        return sellingPrice;
    }
    public void setSellingPrice(Double sellingPrice) {
        this.sellingPrice = sellingPrice;
    }
    public String getColor() {
        return color;
    }
    public void setColor(String color) {
        this.color = color;
    }
    public String getMaterial() {
        return material;
    }
    public void setMaterial(String material) {
        this.material = material;
    }
    public String getStore() {
        return store;
    }
    public void setStore(String store) {
        this.store = store;
    }
    public String getDetails() {
        return details;
    }
    public void setDetails(String details) {
        this.details = details;
    }
    public byte[] getImage() {
        return image;
    }
    public void setImage(byte[] image) {
        this.image = image;
    }
    public String getTrader() {
        return trader;
    }
    public void setTrader(String trader) {
        this.trader = trader;
    }
    public String getStatus() {
        return status;
    }
    public void setStatus(String status) {
        this.status = status;
    }
}
Ani
  • 65
  • 1
  • 2
  • 11

1 Answers1

1

You must first define that is a Large object and also add the type:

@Lob
@Type(type = "org.hibernate.type.BinaryType")
private byte[] image;

You can first try it without @Type

Simon Martinelli
  • 34,053
  • 5
  • 48
  • 82