title: Grails JAX-RS REST multipart file upload
body: <p>Trying to get a multipart file upload to work in a Grails 2.5.4 resource.</p>
  
  <p>I'd really like to use Spring's out of the box multipart features, but I cannot find an example for my particular case.</p>
  
  <p>I am calling the API from Postman. Body with <code>form-data</code> and a single PDF file attached in a <code>file</code> field. No other headers!</p>
  
  <h1>First try</h1>
  
  <pre><code>@POST
  @Path("/pdf")
  Response createPdf(MultipartFile file)
  </code></pre>
  
  <p>Gives back <code>415 Unsuported Media Type</code> in Postman, and this on the server:</p>
  
  <pre><code>[...] ERROR container.ContainerRequest  - A message body reader for Java class org.springframework.web.multipart.MultipartHttpServletRequest [...]
  The registered message body readers compatible with the MIME media type are:
  */* -&gt;
    com.sun.jersey.core.impl.provider.entity.FormProvider
    com.sun.jersey.core.impl.provider.entity.StringProvider
    com.sun.jersey.core.impl.provider.entity.ByteArrayProvider
    com.sun.jersey.core.impl.provider.entity.FileProvider
    com.sun.jersey.core.impl.provider.entity.InputStreamProvider
    com.sun.jersey.core.impl.provider.entity.DataSourceProvider
    com.sun.jersey.core.impl.provider.entity.XMLJAXBElementProvider$General
    com.sun.jersey.core.impl.provider.entity.ReaderProvider
    com.sun.jersey.core.impl.provider.entity.DocumentProvider
    com.sun.jersey.core.impl.provider.entity.SourceProvider$StreamSourceReader
    com.sun.jersey.core.impl.provider.entity.SourceProvider$SAXSourceReader
    com.sun.jersey.core.impl.provider.entity.SourceProvider$DOMSourceReader
    com.sun.jersey.json.impl.provider.entity.JSONJAXBElementProvider$General
    com.sun.jersey.core.impl.provider.entity.XMLRootElementProvider$General
    com.sun.jersey.core.impl.provider.entity.XMLListElementProvider$General
    com.sun.jersey.core.impl.provider.entity.XMLRootObjectProvider$General
    com.sun.jersey.core.impl.provider.entity.EntityHolderReader
    com.sun.jersey.json.impl.provider.entity.JSONRootElementProvider$General
    com.sun.jersey.json.impl.provider.entity.JSONListElementProvider$General
  </code></pre>
  
  <p>Adding <code>@Consumes(MediaType.MULTIPART_FORM_DATA)</code> does not help.</p>
  
  <p>I've tried other params such as <code>MultipartHttpServletRequest</code> but there's still no message body reader.</p>
  
  <h1>Second try</h1>
  
  <pre><code>@POST
  @Path("/pdf")
  @Consumes(MediaType.MULTIPART_FORM_DATA)
  Response createPdf(@FormDataParam("file") InputStream uploadedInputStream,
                         @FormDataParam("file") FormDataContentDisposition fileDetail) 
  </code></pre>
  
  <p>Gives back a 500 error, and this on the server:</p>
  
  <pre><code>SEVERE: Missing dependency for method public javax.ws.rs.core.Response com.carfax.VehicleReceiptPhotoResource.createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at parameter at index 0
  SEVERE: Missing dependency for method public javax.ws.rs.core.Response createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition) at parameter at index 1
  SEVERE: Method, public javax.ws.rs.core.Response createPdf(java.io.InputStream,org.glassfish.jersey.media.multipart.FormDataContentDisposition), annotated with POST of resource, class com.carfax.VehicleReceiptPhotoResource, is not recognized as valid resource method.
  </code></pre>
  
  <p>having these dependencies</p>
  
  <pre><code>compile group: 'com.sun.jersey', name: 'jersey-bundle', version: '1.19.4'
  compile group: 'com.sun.jersey', name: 'jersey-json', version: '1.19.4'
  
  compile "javax.ws.rs:javax.ws.rs-api:2.1@jar"
  compile ('org.glassfish.jersey.media:jersey-media-multipart:2.27') {
      exclude group:'javax.ws.rs'
  }
  compile group: 'org.jvnet.mimepull', name: 'mimepull', version: '1.9.11'
  </code></pre>
  
  <h1>Third try</h1>
  
  <pre><code>@POST
  @Path("/pdf")
  Response createPdf(@FormDataParam("file") InputStream fileInputStream)
  </code></pre>
  
  <p>With the same jersey dependencies as in the second attempt, this works, but it's not good enough for me. I'd like to upload multiple files at once, and get some extra json info on the side.</p>
  
  <h1>What I want</h1>
  
  <ul>
  <li>Some clarification on how many methods of multi file upload are there in Grails</li>
  <li>What are the different libraries achieving this</li>
  <li>To do this in a Grails REST resource, not in a controller</li>
  </ul>
  
  <h1>Some links I went through</h1>
  
  <ul>
  <li><a href="https://stackoverflow.com/questions/8594707/jersey-exception-severe-a-message-body-reader-for-java-class">Jersey Exception : SEVERE: A message body reader for Java class</a></li>
  <li><a href="https://groups.google.com/forum/#!topic/dropwizard-user/moYQOtxXnRU" rel="nofollow noreferrer">https://groups.google.com/forum/#!topic/dropwizard-user/moYQOtxXnRU</a></li>
  <li><a href="https://stackoverflow.com/questions/16831443/maven-jersey-multipart-missing-dependency-for-javax-ws-rs-core-response">Maven jersey-multipart missing dependency for javax.ws.rs.core.Response</a></li>
  <li><a href="https://philsturgeon.uk/api/2016/01/04/http-rest-api-file-uploads/" rel="nofollow noreferrer">https://philsturgeon.uk/api/2016/01/04/http-rest-api-file-uploads/</a></li>
  <li><a href="https://www.javatpoint.com/jax-rs-file-upload-example" rel="nofollow noreferrer">https://www.javatpoint.com/jax-rs-file-upload-example</a></li>
  <li><a href="https://stackoverflow.com/questions/45960689/jax-rs-multipart-with-com-sun-jersey">JAX-RS Multipart with com.sun.jersey</a></li>
  </ul>