javaspring-bootresteasyquarkusquarkus-rest-client

How to send a file from Quarkus to a Spring Boot application?


I'm writing a Quarkus microservice that is meant to communicate with a main Spring Boot application.

In order to make calls to the Spring Boot app, I wrote a REST client based on this Quarkus tutorial and it's working fine for some endpoints. The problem happens when I try to upload a file from Quarkus to Spring boot, I cannot get it to work properly. I followed this other tutorial to work with multipart requests.

Here's my multipart object on my Quarkus application:

public class MultipartBody {
    @FormParam("file")
    @PartType(MediaType.APPLICATION_OCTET_STREAM)
    public InputStream file;

    @FormParam("fileName")
    @PartType(MediaType.TEXT_PLAIN)
    public String fileName;
}

Here's the endpoint on REST client in Quarkus:

@POST
@Path("/file")
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.APPLICATION_JSON)
ProjectFile upload(@HeaderParam(AUTH_HEADER) String apiToken, @MultipartForm MultipartBody data);

Here's how I build the MultipartBody object:

    InputStream stream = IOUtils.toInputStream(contentString, Charset.defaultCharset());
    MultipartBody data = MultipartBody.builder()
            .file(stream)
            .fileName(filename)
            .build();

The endpoint in Spring Boot application:

@PostMapping("/file")
public ProjectFile receive(@RequestParam MultipartFile inputFile)

It throws an error saying that inputFile is not provided:

Required request part 'inputFile' is not present - org.springframework.web.multipart.support.MissingServletRequestPartException - Required request part 'inputFile' is not present

If I change the @RequestParam for @RequestBody, then the inputFile parameter is always null. What am I missing?


Solution

  • Trying to use MultipartFormDataOutput class, multipart form data field is missing file name. In your case, trying to use @PartFilename annotation.

    Must be used in conjunction with @MultipartForm. This defines the filename for a part

    try (InputStream fileInputStream = new FileInputStream(currentFile)) {
    
      MultipartFormDataOutput multipartFormDataOutput = new MultipartFormDataOutput();
    
      multipartFormDataOutput.addFormData("file", fileInputStream, MediaType.APPLICATION_OCTET_STREAM_TYPE, currentFile.getName());
      
      multipartFormDataOutput.addFormData("metaData",objectMapper.writeValueAsString(fileMeta),MediaType.APPLICATION_JSON_TYPE);
    
      // Call client to upload file
    
    }