33
@Path("file.upload")
public class UploadFileService {
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response uploadFile(
        @FormDataParam("file") InputStream uploadedInputStream,
        @FormDataParam("file") FormDataContentDisposition fileDetail) {

    System.out.println("-----------------UploadFileService------------------1.");
    // Should we use a disk or DB? Decided to use DISK
    // Path should be read from properties-files
    String uploadedFileLocation = "//uploaded/" + fileDetail.getFileName();

    // save it
    writeToFile(uploadedInputStream, uploadedFileLocation);

    String output = "File uploaded to : " + uploadedFileLocation;
    // All went OK
    return Response.status(200).entity(output).build();

}
 WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.

SEVERE:   WebModule[/insame]StandardWrapper.Throwable
org.glassfish.jersey.server.model.ModelValidationException: Validation of the application resource model has failed during application initialization.
[[FATAL] No injection source found for a parameter of type public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition) at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=MethodHandler{handlerClass=class com.insame.service.UploadFileService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@47bee27a]}, handlingMethod=public javax.ws.rs.core.Response com.insame.service.UploadFileService.uploadFile(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 javax.ws.rs.core.Response}, nameBindings=[]}']
    at org.glassfish.jersey.server.ApplicationHandler.initialize(ApplicationHandler.java:410)
    at org.glassfish.jersey.server.ApplicationHandler.access$500(ApplicationHandler.java:157)
    at org.glassfish.jersey.server.ApplicationHandler$3.run(ApplicationHandler.java:280)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:289)
    at org.glassfish.jersey.internal.Errors$2.call(Errors.java:286)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:315)
    at org.glassfish.jersey.internal.Errors.process(Errors.java:297)
    at org.glassfish.jersey.internal.Errors.processWithException(Errors.java:286)
    at org.glassfish.jersey.server.ApplicationHandler.<init>(ApplicationHandler.java:277)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:262)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)

I implemented test service like this under the UploadFileService:

@GET
@Path("count")
@Produces("text/plain")
public String countREST() {
    return "1 one 1";
}

and I got this exception to log:

FINE:   [Web-Security] hasResource perm: ("javax.security.jacc.WebResourcePermission" 

"/webresources/file.upload/count" "GET")
SEVERE:   WebModule[/insame]StandardWrapper.Throwable
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
    at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:257)
WARNING:   StandardWrapperValve[com.insame.service.ApplicationConfig]: Allocate exception for servlet com.insame.service.ApplicationConfig
java.lang.IllegalStateException: The resource configuration is not modifiable in this context.
    at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:257)
    at org.glassfish.jersey.server.ResourceConfig$ImmutableState.register(ResourceConfig.java:205)
    at org.glassfish.jersey.server.ResourceConfig.register(ResourceConfig.java:435)
    at org.glassfish.jersey.servlet.WebComponent.<init>(WebComponent.java:261)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:167)
    at org.glassfish.jersey.servlet.ServletContainer.init(ServletContainer.java:349)

Environment

  • Netbeans7.3.1
  • Glassfish 4.0
  • Jersey 2 with Glassfish 4.0
1

6 Answers 6

45

In order to use multipart in your Jersey application you need to register MultiPartFeature in your application, i.e.:

public class ApplicationConfig extends Application {

    public Set<Class<?>> getClasses() {
        final Set<Class<?>> resources = new HashSet<Class<?>>();

        // Add your resources.
        resources.add(UploadFileService.class);

        // Add additional features such as support for Multipart.
        resources.add(MultiPartFeature.class);

        return resources;
    }
}

For more information see Multipart section in the Jersey Users Guide.

For the second issue you're facing try to restart the GlassFish server, I am not sure how NetBeans are reloading the Jersey app after a change (if this doesn't help, please post your ApplicationConfig).

5
  • Thanks a lot! Error message changed and I found one error in my pom.xml. I was using old multipart version in my pom.xml. WELD-001408 Unsatisfied dependencies for type [Providers] with qualifiers [@Default] at injection point [[BackedAnnotatedParameter] Parameter 1 of [BackedAnnotatedConstructor] @Inject public org.glassfish.jersey.media.multipart.internal.MultiPartReaderServerSide(@Context Providers, Provider<CloseableService>)]
    – Sami
    Aug 15, 2013 at 17:57
  • This might be related to having some of the Jersey 2.x bits in your .war (while they're on GF classpath at the same time). Can you mark jersey-media-multipart artifact in your pom.xml as provided? It's already a part of GF 4.0. Aug 15, 2013 at 18:05
  • That was it!! Now it is working! <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-multipart</artifactId> <version>2.2</version> <scope>provided</scope> </dependency>
    – Sami
    Aug 16, 2013 at 12:54
  • I am not using Maven but have the same configuration but the GF4 jersey classes are NOT found ! What can I do ?
    – bashizip
    Feb 19, 2014 at 10:09
  • I had this problem after merging an old Jersey 1 to Jersey 2 project. I think the solution is not the accepted answer, but rather the fix to the POM and web XML files...
    – Hack-R
    Oct 18, 2016 at 11:52
33

I had the same problem and wanted to avoid creating a custom application class. It is not well documented, but if you want to add Multipart functionality, all you have to do is add this to your web.xml jersey servlet config:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

I also added a loggingfilter.

3
  • Your solution is the BEST! Mar 30, 2015 at 13:20
  • 1
    I can't get this to work either. Does it need to be somewhere specific order-wise? Do you have a gist of your entire pom by any chance? Jul 16, 2015 at 20:51
  • This is the easiest approach and works like a charm! Highly recommended!
    – Ozil
    Mar 14, 2017 at 10:03
2

If you are using jetty server and jersey servlet, then you can solve this problem by adding the following code in your main class where you have started the jetty server,

ServletHolder jerseyServlet = context.addServlet( org.glassfish.jersey.servlet.ServletContainer.class, "/*"); jerseyServlet.setInitOrder(0);

      // Tells the Jersey Servlet which REST service/classes to load.
      jerseyServlet
              .setInitParameter(
                      "jersey.config.server.provider.classnames",
                      <Your entry point class's canonical name>
                              + ";org.glassfish.jersey.media.multipart.MultiPartFeature");
0

Just minor clarification

Use

import org.glassfish.jersey.media.multipart.MultiPartFeature
import org.glassfish.jersey.media.multipart.FormDataContentDisposition;
import org.glassfish.jersey.media.multipart.FormDataParam;

Not

com.sun.jersey.*

Did work for me only when used org.glassfish.jersey.media.multipart.*

In ApplicationConfig just register MultiPartFeature as

import org.glassfish.jersey.media.multipart.MultiPartFeature;

@javax.ws.rs.ApplicationPath("webresources")
public class ApplicationConfig extends Application {

    @Override
    public Set<Class<?>> getClasses() {
        Set<Class<?>> resources = new java.util.HashSet<>();
        resources.add(UploadFileService.class);
        resources.add(MultiPartFeature.class);
        return resources;
    }
}
1
  • Hlleo guys,i'm using wildfly server, can i use import org.glassfish.jersey.media.multipart.* in my ApplicationConfig , if yes can you tell what is the dependency that i should add in my pom.xml , because im getting this error in my project : javax.ws.rs.NotSupportedException: Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form-data;, thak you in advanced.
    – James
    Jun 19, 2016 at 18:19
0

I'm using Jersey 1.9.1. org.glassfish...... works well with Jersey 2. For Jersey 1 you better use com.sun... classes.

1
  • 1
    Instead of adding two posts for your answer, I'd suggest that you edit your post of your first attempt to answer the question (i.e. this post). Thanks.
    – lrnzcig
    Mar 23, 2017 at 14:06
-1

You can use @FormDataParam("file") equivalent of FormDataMultiPart if you want it using annotation.

Used as given below sample code extract:

public Response uploadFile( **@FormDataParam("file")** InputStream fileInputStream,
             @FormDataParam("file") FormDataContentDisposition contentDispositionHeader) {

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.