在Spring Boot API中,我正在构建一个用于更改封面照片的端点。我将来自前端的一些数据作为ChangeCoverRequest
对象发送给文件。但是,当我发送请求时,Spring Boot会抛出以下错误。
com.fasterxml.jackson.databind.exc.InvalidDefinitionException:无法 构造的实例
org.springframework.web.multipart.MultipartFile
(没有创造者,例如 默认构造,存在):抽象类型要么需要映射到 具体类型,具有自定义反序列化器或包含其他类型 信息
我知道字段private MultipartFile cover
是导致此错误的原因,但我不知道如何构造ChangeCoverRequest
以便不会引发此错误。 / p>
谢谢。
ChangeCoverRequest
public class ChangeCoverRequest {
private Long userId;
private Long groupId;
private MultipartFile cover;
public ChangeCoverRequest() {
}
public ChangeCoverRequest(Long userId, Long groupId, MultipartFile cover) {
super();
this.userId = userId;
this.groupId = groupId;
this.cover = cover;
}
public Long getUserId() {
return userId;
}
public void setUserId(Long userId) {
this.userId = userId;
}
public Long getGroupId() {
return groupId;
}
public void setGroupId(Long groupId) {
this.groupId = groupId;
}
public MultipartFile getCover() {
return cover;
}
public void setCover(MultipartFile cover) {
this.cover = cover;
}
}
更改封面端点
@PostMapping("/group/change-cover")
public void changeGroupCoverPhoto(@RequestBody ChangeCoverRequest changeCovReq) {
String fileName = fileService.storeFile(changeCovReq.getCover());
String fileDownloadUri = ServletUriComponentsBuilder.fromCurrentContextPath()
.path("/api/downloadFile/")
.path(fileName)
.toUriString();
UploadFileResponse response = new UploadFileResponse(fileName, fileDownloadUri,
changeCovReq.getCover().getContentType(), changeCovReq.getCover().getSize());
Group group = groupService.getGroup((long)changeCovReq.getUserId()).get();
group.setCoverPhoto(response.getFileDownloadUri());
groupRepository.save(group);
}