簡體   English   中英

在響應中發送包含blob的實體對象

[英]Sending Entity Object in Response which contains blob

我正在嘗試創建一個springboot用戶管理應用程序。

我有一個包含兩個blob元素的實體對象。這是我的實體對象。

 @Entity
    @Table(name="user_meta_profile")
    public class UserMetaProfile implements Serializable {
        private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "user_id")
    private int user_id;

    @Column(name = "resume_file")
    @Lob
    private Blob resume_file;

    @Column(name = "photo")
    @Lob
    private Blob photo;

    @Column(name = "username")
    private String username;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    public Blob getResume_file() {
        return resume_file;
    }

    public void setResume_file(Blob resume_file) {
        this.resume_file = resume_file;
    }

    public Blob getPhoto() {
        return photo;
    }

    public void setPhoto(Blob photo) {
        this.photo = photo;
    }

   public void setUsername(String username) {
        this.username = username;
    }
}

如您所見,有兩個Blob項目“ resume_file”和“ photo”。

我想將JSON響應發送回API調用。

我的控制器代碼如下所示。

 @Controller
    @RequestMapping("/v1")
    public class UsersController {

    @Autowired 
        private IUserMetaProfileService userMetaProfileService;


    @GetMapping("MetaProfile/{id}")
        public ResponseEntity<UserMetaProfile> getUserMetaProfileById(@PathVariable("id") Integer id) {
            UserMetaProfile userMetaProfile = userMetaProfileService.getUsersById(id);
            return new ResponseEntity<UserMetaProfile>(userMetaProfile, HttpStatus.OK);
        }

    }

但是當我調用API時,出現了異常:

"exception": "org.springframework.http.converter.HttpMessageNotWritableException",

 "message": "Could not write JSON document: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) (through reference chain:
...

   ...nested exception is com.fasterxml.jackson.databind.JsonMappingException: No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS)

由於JSON無法包含二進制數據,因此您需要將這些字段序列化為其他內容。 您有兩種選擇:

  1. 如果您打算將二進制文件顯示為圖像(因為您的照片是照片),則可以將其序列化為數據uri。
  2. 而是發送指向照片的鏈接,並創建一個控制器方法,該方法將輸出具有適當內容類型的二進制數據(超出此處的范圍)。

因此,對於選項1,您可以執行以下操作:

@Entity
@Table(name="user_meta_profile")
public class UserMetaProfile implements Serializable {

    private static final long serialVersionUID = 1L;

    @Id
    @Column(name = "user_id")
    private int user_id;

    @Column(name = "resume_file")
    @Lob
    private Blob resume_file;

    @Column(name = "photo")
    @Lob
    private Blob photo;

    @Column(name = "username")
    private String username;

    public int getUser_id() {
        return user_id;
    }

    public void setUser_id(int user_id) {
        this.user_id = user_id;
    }

    @JsonIgnore // disable serializing this field by default
    public Blob getResume_file() {
        return resume_file;
    }

    // serialize as data uri insted
    @JsonProperty("resumeData")
    public String getResume() {
      // just assuming it is a word document. you would need to cater for different media types
      return "data:application/vnd.openxmlformats-officedocument.wordprocessingml.document;base64," + new String(Base64.getEncoder().encode(resume_file.getBytes()));
    }

    public void setResume_file(Blob resume_file) {
        this.resume_file = resume_file;
    }

    @JsonIgnore // disable this one too
    public Blob getPhoto() {
        return photo;
    }

    // serialize as data uri instead
    @JsonProperty("photoData")
    public String getPhotoBase64() {
      // just assuming it is a jpeg. you would need to cater for different media types
      return "data:image/jpeg;base64," + new String(Base64.getEncoder().encode(photo.getBytes()));
    }

    public void setPhoto(Blob photo) {
        this.photo = photo;
    }

   public void setUsername(String username) {
        this.username = username;
    }
}

對於照片位,可以將photoData JSON屬性的值直接設置為img標簽的src屬性,並且照片將以HTML呈現。 使用簡歷文件,您可以將它作為href附加到具有download屬性的<a>標記,以便可以下載:

<a href={photoData value here} download>Download Resume File</a>

僅供參考,如果文件很大,JSON也會很大,這也可能會降低瀏覽器的速度。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM