【问题标题】:Multipart POST keeps failing in Springboot due to Content-Type header由于 Content-Type 标头,Multipart POST 在 Spring Boot 中不断失败
【发布时间】:2018-10-24 07:29:43
【问题描述】:
我正在尝试使用 Spring-boot 创建一个 REST API,该 API 在内部与某个其他应用程序的 Java 控制器通信。当我尝试使用 POSTMAN 将图像设置 Content-Type 发布为 multipart/form-data 时,我不断收到错误 500“java.io.IOException:缺少初始多部分边界”。我浏览了其他一些博客,说发生这种情况是因为当我们手动将 Content-Type 覆盖到标题时会删除边界 =“”,因此我们应该避免使用它。我在不使用标头的情况下尝试了相同的操作,但随后我收到 400 提示“无效的请求标头。访问被拒绝。”。有没有人有相同的解决方法?提前致谢。
【问题讨论】:
标签:
spring-boot
postman
request-headers
【解决方案1】:
不是直接回答您的问题,但我认为您可以尝试在您的项目中使用 Swagger,工作量不大,您将获得项目的 API,并且您可以尝试使用微调的默认值调用您的服务(像标题中的 Content-Type)。
【解决方案2】:
检查这是否有帮助,使用 content-type 作为 application/json
@RestController("/image")
public class TestControllerEx {
@PostMapping
public ResponseEntity image(@RequestBody Image image){
System.out.println("Got image..."+image.getImage());
return new ResponseEntity("success", HttpStatus.CREATED);
}
}
public class Image {
private byte[] image;
public byte[] getImage() {
return image;
}
public void setImage(byte[] image) {
this.image = image;
}
}
在 Postman 正文中输入:
{"image":"dGVzdCBtZXNzYWdl"}