我正在嘗試創建一個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)
您嘗試將二進制文件添加到JSON字符串。這不可能。如果您真的想將二進制文件添加到JSON字符串中,則必須對二進制文件進行base64編碼。 – Eich