[英]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,您可以執行以下操作:
@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.