How to create MultipartFile object from InputStream in Spring Boot

Viewed 5725

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

0 Answers
Related