Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to transmit Multipart file object into Spring web RestClient post method

I have two microservices:

  • One for user authentication (authentication microservice).
  • One for handling video file storage (videoStore microService).

Process :

  • ("/create") belongs to authentication microservice
  • ("/create") is accessible only with well formed token (JWT).
  • ("/create") receives multipartFile
  • ("/create") sends multipartFile to storage service
  • ("/uploadfile") belongs to file storage microservice
  • ("/uploadfile") performs the storage of the file into linux filesystem
@PostMapping(value ="/create")
 ResponseEntity<Response> createVideo(@RequestParam("file") MultipartFile multipartFile) 
 {  
    videoRestClientService.upload(multipartFile);
    return new ResponseEntity<Response>(new Response("video has been stored"), HttpStatus.OK);
 }
public Response upload(MultipartFile multipartFile){
    
    RestClient restClient = RestClient.create("http://localhost:8095");
    Response response = restClient.post()
     .uri("/uploadfile")
     .contentType(...)
     .body(multipartFile)
     .retrieve()
     .body(Response.class);
}

-> In videoStore microService

@PostMapping("/uploadfile") 
public ResponseEntity<Response> uploadFile(@RequestParam("file") MultipartFile multipartFile) {

    Response response = fileSystemStorage.storeFile(multipartFile);

    return new ResponseEntity<Response>(response, HttpStatus.OK);
}
<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot</artifactId>
    <version>3.2.1</version>
</dependency>
<dependency>
    <groupId>org.springframework</groupId>
    <artifactId>spring-web</artifactId>
    <version>6.1.2</version>
</dependency>

Question :

  • Is it possible to use restClient to post multipartFile ?

Remark :

  • I don't have any getway implemented (don't have the skills yet).
  • The authentication service is doing the getway job (to bad for clean archicture).
like image 295
KnowledgePath Avatar asked Nov 02 '25 19:11

KnowledgePath


1 Answers

Is it possible to use restClient to post multipartFile ?

I don't know if you have solved this yet. But the answer is yes. I was doing the same thing and have just made it works after a day of researching. Here is what you need:

import org.springframework.web.client.RestClient;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.core.io.Resource;
import org.springframework.http.MediaType;

 
public Response upload (MultipartFile multipartFile) {
    // I am not create RestClient like this. I use @Autowired instead
    RestClient restClient = RestClient.create("http://localhost:8095");
    MultiValueMap<String, Resource> body = new LinkedMultiValueMap<>();
    body.add("file", multipartFile.getResource());
    Response response = restClient.post()
            .uri("/uploadfile")
//            .contentType(MediaType.MULTIPART_FORM_DATA)  // Not need
            .body(body)
            .retrieve()
            .body(Response.class);
    return response;
}
like image 116
Nguyen Nguyen Avatar answered Nov 04 '25 09:11

Nguyen Nguyen