1

I am developing a REST web service using Java and Quarkus framework. I would like to pass the InputStream as a datatype to my Rest Resource method as the method executed by the application accepts only InputStream as a input.

When I make the datatype as String and later convert it to InputStream then everything works out fine but only problem is that I need to convert the input from String->InputStream within the code every time.

I wanted to know if it's possible to accept the InputStream itself as a datatype for the Rest Resource API method so I can avoid the conversion from String to InputStream. When I do it I get the option only to read file in my Swagger-UI and method also not executed due to the InputStream data type.

Following is the current code I have which is working perfectly:

@Path("/api")
public class ConverterResource {

    @POST
    @Path("/converter")
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> fileConverter(@HeaderParam("Content-Type") final String contentType, final String inputDocument) {
        final InputStream inputDocumentStream = new ByteArrayInputStream(inputDocument.getBytes(StandardCharsets.UTF_8));
        final TestClass testClass = new TestClass();
        return contentType.equals("application/xml") ? testClass.xmlConverter(inputDocumentStream) : eventHashGenerator.jsonConverter(inputDocumentStream);
    }
}

I would like to achieve something like this so conversion can be avoided:

@Path("/api")
public class ConverterResource {

    @POST
    @Path("/converter")
    @Consumes({MediaType.APPLICATION_JSON, MediaType.APPLICATION_XML})
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> fileConverter(@HeaderParam("Content-Type") final String contentType, final InputStream inputDocumentStream) {
        //final InputStream inputDocumentStream = new ByteArrayInputStream(inputDocument.getBytes(StandardCharsets.UTF_8));
        final TestClass testClass = new TestClass();
        return contentType.equals("application/xml") ? testClass.xmlConverter(inputDocumentStream) : eventHashGenerator.jsonConverter(inputDocumentStream);
    }
}

I quite new to Quarkus so not sure exactly if its possible or I am doing something wrong. Any help or suggestion would be really helpful. Thanks a lot in advance.

BATMAN_2008
  • 2,788
  • 3
  • 31
  • 98

1 Answers1

0
  1. Add dependency for multipart support

    <groupId>io.quarkus</groupId>
    <artifactId>quarkus-resteasy-multipart</artifactId>
    
  2. Write your Resource like :

@Path("/api")
public class ConverterResource {

    @POST
    @Path("/converter")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    @Produces(MediaType.APPLICATION_JSON)
    public List<String> fileConverter(@HeaderParam("Content-Type") final String contentType, @MultipartForm MultipartFormDataInput multipartInput) {
        final InputStream inputDocumentStream = multipartInput.getFormDataPart("file", InputStream.class, null);
        final TestClass testClass = new TestClass();
        return contentType.equals("application/xml") ? testClass.xmlConverter(inputDocumentStream) : eventHashGenerator.jsonConverter(inputDocumentStream);
    }
}

Hope this help you

DV Singh
  • 1,038
  • 11
  • 16