Java 正在响应中发送包含blob的实体对象

Java 正在响应中发送包含blob的实体对象,java,json,spring-boot,blob,httpresponse,Java,Json,Spring Boot,Blob,Httpresponse,我正在尝试创建一个springboot用户管理应用程序 我有一个实体对象,它包含两个blob元素 @Entity @Table(name="user_meta_profile") public class UserMetaProfile implements Serializable { private static final long serialVersionUID = 1L; @Id @Column(name = "user_id")

我正在尝试创建一个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\u 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);
        }

    }

由于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附加到
    
    

    仅供参考,如果文件很大,JSON会很大,并且可能会降低浏览器的速度。

    您尝试将二进制文件添加到JSON字符串中。这是不可能的。如果您真的想将二进制文件添加到JSON字符串中,则必须对二进制文件进行base64编码。
    @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;
        }
    }
    
    <a href={photoData value here} download>Download Resume File</a>