[英]MissingServletRequestPartException: Required request part 'file' is not present Springboot
I have a controller that performs file upload , i'm trying to post a request to controller endpoint from another service.我有一个执行文件上传的控制器,我正在尝试从另一个服务向控制器端点发布请求。
@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;
}
Code from where i'm calling the above endpoint我调用上述端点的代码
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;
}
I'm getting the below error, don't know the reason:我收到以下错误,不知道原因:
[MissingServletRequestPartException: Required request part 'file' is not present]
Been looking around for a while with no solution.找了半天没解决。
Probably, you are missing file
param within your request, so you must consider file
param inside of your request.可能您的请求中缺少file
参数,因此您必须考虑请求中的file
参数。 Also you can add new attribute ( require
) inside @RequestParam
such as @RequestParam(value = "file", required = false)
.您也可以在@RequestParam
添加新属性( require
),例如@RequestParam(value = "file", required = false)
。 Purpose of required
attribute, to make request parameter to be required (if true) or optional (if false). required
属性的目的,使请求参数成为必需(如果为真)或可选(如果为假)。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.