6

I have 2 services ServiceA and ServiceB, ServiceB has one API called createDocument(@RequestParam("file") MultipartFile file). Now I want to call this API from ServiceA and I am using FeignClient and both services use Spring Boot.

My FeignClient looks something like this

@FeignClient(
    name = "serviceB",
    url = "com.serviceb.url",
    configuration = {ServiceBErrorDecoder.class},
    fallbackFactory = ServiceBFallbackFactory.class)
@Service
public interface ServiceB{

@PostMapping(value = "/documents")
    ResponseEntity createDocument(@RequestParam("file") MultipartFile file);
}

And now, in ServiceA, I want to call this createDocument API. I can do that but problem is how can I create MultipartFile object and pass it? I have InputStream object having some content and I don't want to create physical file.

ServicA code

public void processObjects(Entity entity) throws JsonProcessingException {
    SomeObject object = new SomeObject();
    //setters
    ....
    InputStream inputStream = new ByteArrayInputStream(
            Objects.requireNonNull(SerializationUtils.serialize(object)));
    InputStreamResource inputStreamResource = new InputStreamResource(inputStream);
    
    //How do I create MultipartFile object from this InputStream?
    serviceB.createDocument(**multipartfile**)
}

Please help. Thank you

Pavan
  • 217
  • 1
  • 4
  • 12
  • The MultipartFile is created by Spring itself, from input request. Sure it's still possible to instantiate it programmatically, but in that case, 'InputStream' should contain whole multipart request body, exact as it was received. So think in your case will be easier to create another method that accepts 'inputStream' as a parameter and call it. – Alex Chernyshev Aug 12 '20 at 13:45
  • But, I cannot modify the method or add other method. I want to instantiate it programmatically. I know all multiparts which i need to add in request body, but not able to match that MultipartFile type. Could you please let me know any code samples? – Pavan Aug 12 '20 at 13:52
  • 2
    https://stackoverflow.com/questions/18381928/how-to-convert-byte-array-to-multipartfile – Alex Chernyshev Aug 12 '20 at 13:53
  • Thank you very much for the SO reference. That solution is my last hope. I thought there could be some library or something so that I don't have to do that. But, no luck. Thanks man for answering. I appreciate it. – Pavan Aug 12 '20 at 13:59

0 Answers0