在包含 blob 的响应中发送实体对象
Sending Entity Object in Response which contains blob
我正在尝试创建一个 springboot 用户管理应用程序。
我有一个实体对象,其中包含两个 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'。
我想发回对 API 调用的 JSON 响应。
我的控制器代码如下图
@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 不能包含二进制数据,您需要将这些字段序列化为其他内容。您有几个选择:
- 如果您打算将二进制文件显示为图像(因为您的是照片),您可以将其序列化为数据 uri。
- 改为发送照片链接并创建一个控制器方法,该方法将输出具有适当内容类型的二进制数据(超出此处的范围)。
所以对于选项 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 会很大,而且还可能会降低浏览器速度。
我正在尝试创建一个 springboot 用户管理应用程序。
我有一个实体对象,其中包含两个 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'。
我想发回对 API 调用的 JSON 响应。
我的控制器代码如下图
@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 不能包含二进制数据,您需要将这些字段序列化为其他内容。您有几个选择:
- 如果您打算将二进制文件显示为图像(因为您的是照片),您可以将其序列化为数据 uri。
- 改为发送照片链接并创建一个控制器方法,该方法将输出具有适当内容类型的二进制数据(超出此处的范围)。
所以对于选项 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 会很大,而且还可能会降低浏览器速度。