Yavin
Yavin

Reputation: 455

Payara/Glassfish JAX-RS multipart

I'm trying to setup jax-rs multipart endpoint on paraya (I think same will be with glassfish). I've made simple example with just a minimal java-ee8 code. I know that there is no standard way for adding multipart support to jax-rs.

I found that for payara/glassfish I must add MultiPartFeature class like this:

@ApplicationPath("/api")
public class JAXRSConfiguration extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(MultiPartFeature.class);
        return classes;
    }
}

This is my endpoint:

@Path("")
public class ExampleResource {
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public String postMultipartMethod(
        @FormDataParam("field") InputStream fileInputStream,
        @FormDataParam("field") FormDataContentDisposition fileMetaData
    ) {
        return "post multipart method";
    }
}

.

curl -v -F [email protected] http://localhost:8080/jax-rs-multipart/api
> POST /jax-rs-multipart/api HTTP/1.1
> Content-Type: multipart/form-data; boundary=------------------------d60a2c38aa57dfbe
> 
< HTTP/1.1 404 Not Found

I'm getting "404 - The requested resource is not available". If I comment out adding MultiPartFeature and method fields, then endpoint is working, but I can't get posted fields. If I comment out MultiPartFeature and leave method parameters then I get "No injection source found for a parameter of type ..."

It deploys fine don't see enything wrong in logs.

I put it in github repo if you want to reproduce.

Upvotes: 0

Views: 565

Answers (1)

Vladislav Balashov
Vladislav Balashov

Reputation: 21

You need also add yours resources classes (annotated with @Path)

@ApplicationPath("/api")
public class JAXRSConfiguration extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> classes = new HashSet<>();
        classes.add(MultiPartFeature.class);
        classes.add(YourResource.class);
        return classes;
    }
}

EDIT: But I found better solution: Create class like this

@Provider
public class MultiPartFeatureProvider extends MultiPartFeature {
}

Upvotes: 1

Related Questions