how to validate multipart file name if the part is not required

39 Views Asked by At

In the following code, I have the second requestpart as optional, but the problem i'm facing is that if the client sends any value other than "testAttachment", there is no binding error and still get successful response, but the actual file attached in the client doesn't get updated to the server. I guess this is because the MultipartFile is optional, hence the multipartFile attachment object always returns null. The question is I cannot change the second part to become required because of the existing contract, but how can I validate the second part value/name by throwing an error if the name is not "testAttachment". Thanks!

@PostMapping(value = "/myapi/test", consumes = MediaType.MULTIPART_FORM_DATA_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
  public ResponseEntity<TestResponseDto> processTestMultiparts(
      @RequestPart(value = "testData") TestRequestDto testRequestDto,
      @RequestPart (value = "testAttachment", required = false) MultipartFile attachment,
      BindingResult bindingResult)
      throws APIException, RemoteException, AuthorizationException
  {
   
    return processMyTest(testRequestDto, attachment, bindingResult);
  }

I have tried to use name attribute in the second part name = "testAttachment", also create a custom_validator, but attachment always returns null, and the only method I tried that works is to make the second part is required, but that's not the option. I need some help and guidence as to how validate the second part value/name and throw an error if the name is not "testAttachment".

0

There are 0 best solutions below