I'm creating upload service with Jersey 2.5.1.
public class JerseyApplication extends ResourceConfig {
public JerseyApplication() {
register(RequestContextFilter.class);
register(MultiPartFeature.class);
packages("com.my.app");
packages(JerseyApiDeclarationProvider.class.getPackage().getName());
register(JacksonFeature.class);
}
}
and the Resource
for the upload is as follows:
@Path("api/admin/image")
@Produces(APPLICATION_JSON)
@Component
public class ImageUploadResource {
@POST
@Path("/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public String upload(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {
return contentDispositionHeader.getFileName();
}
}
My Maven dependencies include
<dependency>
<groupId>org.glassfish.jersey.media</groupId>
<artifactId>jersey-media-multipart</artifactId>
</dependency>
<dependency>
<groupId>org.glassfish.jersey.containers</groupId>
<artifactId>jersey-container-servlet</artifactId>
</dependency>
But starting up the application fails with:
[FATAL] No injection source found for a parameter of type public java.lang.String com.my.app.resource.ImageUploadResource.upload(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[application/json], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.my.app.resource.ImageUploadResource, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@1f5894ee]}, definitionMethod=public java.lang.String com.sanoma.avain.resource.ImageUploadResource.upload(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition), parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null]], responseType=class java.lang.String}, nameBindings=[]}']
at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:444)
From all the examples I've found this should be pretty standard way and I've only found solutions where they were missing the jersey-media-multipart
dependency.
Well, this was a typical PEBCAC. We had Swagger as a dependency in our project. And Swagger uses a really old version of Jersey and an old version of Jersey2. So I ended up with a project where some of my imports where from one Jersey version and rest from another. This causes lots of weird behavior as things work almost but not quite.