我偶然发现了类似的问题,并且了解到@RequestBody无法在Spring中与多部分文件一起发送。谁能解释为什么?
有人建议对数据和文件使用@RequestPart,我接受了它。 我的控制器如下:
@PostMapping(value = "localhost:8080/medicine", consumes = {"multipart/form-data"})
@ResponseBody
public BooleanResponse saveMedicineData(@RequestPart(value = "file") MultipartFile file, @RequestPart Medicine medicine) {
...
}
医学是带有Lombok批注的实体类。其实现如下:
@Data
@Table(name = "medicine")
@Entity
@Builder
@NoArgsConstructor
@AllArgsConstructor
public class Medicine {
private Long id;
private String name;
private String url;
}
在邮递员对此URL发出发布请求后,我得到了以下例外情况:
{
"timestamp": 1533476503168,
"status": 415,
"error": "Unsupported Media Type",
"exception":
"org.springframework.web.HttpMediaTypeNotSupportedException",
"message": "Content type 'application/octet-stream' not supported",
"path": "/medicine"
}
如何处理此错误?