2017-06-28 48 views
0

我正在嘗試創建一個springboot用戶管理應用程序。在包含blob的響應中發送實體對象

我有一個實體對象,其中包含兩個blob elements.Here是我的實體對象。

@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) 
+0

您嘗試將二進制文件添加到JSON字符串。這不可能。如果您真的想將二進制文件添加到JSON字符串中,則必須對二進制文件進行base64編碼。 – Eich

回答

1

由於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的<a>標籤與download屬性,因此它可以下載:

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

正如一個供參考,如果文件很大的JSON將是巨大的,它也可能減慢瀏覽器。

+0

謝謝,Strelok.It工作。 – codeLearner

相關問題