【问题标题】:Bytea persist and retrieval in Postgresql with Spring Data JPA and AngularBytea 使用 Spring Data JPA 和 Angular 在 Postgresql 中持久化和检索
【发布时间】:2021-06-07 13:54:34
【问题描述】:

我正在将一个数组从我的 Angular 前端发送到 Spring Boot 后端,这个数组作为字节保留在 postgresql 数据库中,但是当我通过从前端到后端的请求检索它时,我得到一个字符串。

如果有人能对此有所了解,我将不胜感激。

发送到后端的内容:

{
     "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"
}

包含图像字段的我的实体类是:

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;
    }
}

【问题讨论】:

  • 你是如何用 JPA 映射图像的?
  • 我只是将一个字节数组从我的角度传递到我的 Spring 后端,所以基本上我没有向它发送任何图像。
  • 但是你有一个实体类?请显示一些代码stackoverflow.com/help/how-to-ask
  • @SimonMartinelli 我已将实体类的代码添加为我的问题的一部分。谢谢

标签: angular postgresql spring-boot bytea


【解决方案1】:

您必须首先定义它是一个大对象并添加类型:

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

不用@Type也可以先试试

【讨论】:

  • 我试过了,但它仍然返回get请求响应中的字符串。
  • 以及如何将字节数组转换为响应中的图像?
  • 我没有做,我只是在 Postman 中检查对请求的响应
  • 在这种情况下,字节数组只是简单地转换为字符串
猜你喜欢
  • 1970-01-01
  • 2014-01-08
  • 2012-07-02
  • 2019-05-08
  • 1970-01-01
  • 2014-10-20
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多