MissingServletRequestPartException:必需的请求部分'file'不存在 Springboot



>我有一个执行文件上传的控制器,我正在尝试从另一个服务向控制器端点发布请求。

@RequestMapping(path = "/upload/{id}", method = RequestMethod.POST)
public String uploadBaseImage(@RequestParam("data") String imageData, @RequestParam("file") MultipartFile file,@PathVariable("id") String id)
throws Exception {
String imageUrl = feedHandler.UploadAndSetImageUrl(imageData, file,Integer.parseInt(id));
return imageUrl;
}

我调用上述端点的代码

public SupplierFeedResponse uploadBaseFeedImage(String data, MultipartFile file, String supplierId) throws IOException {
String uploadBaseFeedEndpoint = uploadFeedEndpoint+Constants.FEED_SERVICE_BASE_FEED_UPLOAD_URI+supplierId;
SupplierFeedResponse supplierFeedResponse =  new SupplierFeedResponse();
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.MULTIPART_FORM_DATA);
MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
body.add("file", new ByteArrayResource(file.getBytes()));
body.add("data", data);
log.info("Request body : "+body.toString());
HttpEntity<MultiValueMap<String, Object>> requestEntity = new HttpEntity<>(body, headers);
RestTemplate restTemplate = new RestTemplate();
ResponseEntity<String> response = restTemplate.postForEntity(uploadBaseFeedEndpoint, requestEntity, String.class);
return supplierFeedResponse;
}

我收到以下错误,不知道原因:

[MissingServletRequestPartException: Required request part 'file' is not present]

四处寻找了一段时间,没有解决方案。

可能是请求中缺少file参数,因此您必须考虑请求中的file参数。您也可以在@RequestParam中添加新属性(require(,例如@RequestParam(value = "file", required = false)required属性的用途,使请求参数成为必需参数(如果为真(或可选参数(如果为假(。

最新更新