繁体   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