Multipart Support

Currently, the service-generator does not explicitly support multipart data. However, you can still use multipart data in your RAML definition and use the generated JAX-RS annotated class.

If your RAML file contains an endpoint definition like the following:

/test:
    post:
        body:
            multipart/form-data:
                formParameters:
                    file:
                        type: file
                    text:
                        type: string

Then the method signature generated in the JAX-RS class does not contain any body parameters.

@Singleton
public class DefaultTestResource implements TestResource
{
    @Context
    private UriInfo uriInfo;

    /* POST / */
    @Override
    public Response post()
    {
        return Response.created(uriInfo.getAbsolutePath()).build();
    }
}

To access the content of the multipart form inside the generated method, perform the following steps:

  1. Add a dependency to Jersey's multipart library:
    <dependency>
     <groupId>org.glassfish.jersey.media</groupId>
     <artifactId>jersey-media-multipart</artifactId>
     <version>${jersey.version}</version>
    </dependency>
    
  2. Register the MultiPartFeature in your application:
    register(MultiPartFeature.class);
    
  3. Add a member variable of the type ContainerRequest to your class annotated with @Context:
    @Context
    private ContainerRequest request;
    
  4. Remove the @Singleton annotation from the resource class, as the ContainerRequest variable is not injected per the request, but rather per the resource class instantiation.
  5. Get form data from the request variable within the method related to the multipart request:
public class DefaultTestResource implements TestResource
{
    @Context
    private UriInfo uriInfo;

    @Context
    private ContainerRequest request;

    /* POST / */
    @Override
    public Response post()
    {
        final FormDataMultiPart part = request.readEntity(FormDataMultiPart.class);
        final String field = part.getField("text").getValue();
        System.out.println("My Form: text="+field);
        return Response.created(uriInfo.getAbsolutePath()).build();
    }
}