0

I am new to Helidon MP and would like to know if there is a way to upload files to Oracle DB (Blob/Clob Column) via REST service created using Helidon MP.

I am able to acheive the same requirement in SpringBoot using below code accepting the file parameter as MultiPart, how can we acheive this using Helidon MP

@PostMapping("/upload")
      public ResponseEntity<ResponseMessage> uploadFile(@RequestParam("file") MultipartFile file) {
        String message = "";
        try {
          storageService.store(file);

          message = "Uploaded the file successfully: " + file.getOriginalFilename();
          return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message));
        } catch (Exception e) {
          message = "Could not upload the file: " + file.getOriginalFilename() + "!";
          return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message));
        }
      }    
Gaurav
  • 53
  • 3
  • From the "About" page: "*Helidon is a collection of Java libraries for writing microservices...*" So of course there is a way to do it, but you need to provide more details of what did you try and what was wrong with your code/attempts. – astentx Nov 30 '20 at 16:34
  • Hi i have done the same requirement using SpringBoot and i could acheive the same by accepting the parameter as MultipartFile and do the corresponding processing for the same. In Helidon MP i am not sure how would we accept the file as @RequestParam is a SpringBoot component.Below code shows how i have acheived the same using SpringBoot – Gaurav Nov 30 '20 at 16:41
  • public ResponseEntity uploadFile(@RequestParam("file") MultipartFile file) { String message = ""; try { storageService.store(file); message = "Uploaded the file successfully: " + file.getOriginalFilename(); return ResponseEntity.status(HttpStatus.OK).body(new ResponseMessage(message)); } catch (Exception e) { message = "Could not upload the file: " + file.getOriginalFilename() + "!"; return ResponseEntity.status(HttpStatus.EXPECTATION_FAILED).body(new ResponseMessage(message)); } } – Gaurav Nov 30 '20 at 16:42
  • Please, add this information to the question via [edit](https://stackoverflow.com/posts/65075371/edit), not to comments. – astentx Dec 01 '20 at 06:19

1 Answers1

0

Helidon offers support for multi-part data through both MP and SE (with buffered and streamed API version for SE) flavors.

The implementation relies on Jersey Integration with MIME MultiPart messages (would be quite similar to any JAX-RS provider).

An implementation of a controller endpoint method allowing to upload files is straightforward and looks like below:

import import javax.ws.rs.*;
import org.glassfish.jersey.media.multipart.*;

@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response upload(MultiPart multiPart) throws IOException {

    multiPart.getBodyParts().stream()
            .filter(part -> /* retrieve your file part here, should be based on the `Content-Disposition` name parameter */ )
            .map(filePart -> {
                String fileName  = pfilePartart.getContentDisposition().getFileName();
                InputStream is = filePart.getEntityAs(BodyPartEntity.class).getInputStream();
                // use the injected `storageService` to somehow store the input stream
                this.storageService.store(fileName, is);
            });
    
}

Note that the JAX-RS multi-part feature is not activated by default, hence should be activated using JAX-RS providers:

import javax.ws.rs.core.*;
import javax.ws.rs.ext.Provider;

@Provider
public class MultiPartFeatureProvider implements Feature {

    @Override
    public boolean configure(FeatureContext context) {
        return new MultiPartFeature().configure(context);
    }
}
scuro
  • 828
  • 8
  • 12
tmarwen
  • 15,750
  • 5
  • 43
  • 62
  • You made a little error in your class name. This example is how it should be done: https://github.com/oracle/helidon/blob/2.3.2/examples/microprofile/multipart/src/main/java/io/helidon/examples/microprofile/multipart/MultiPartFeatureProvider.java Note; the Jersey multipart dependency is required in the pom.xml – scuro Mar 15 '22 at 10:29