Question

I am using glassfish 4 to build some restful apps using the standard Java 7EE stack recently released.

My basic restful service works fine, but now I want to handle incoming file uploads which use the multipart mime type within the same service.

I found a POC jersey maven example (multipart-webapp) referred to in the https://jersey.java.net/documentation/latest/user-guide.html and this deploys and works fine. However, as soon as I build on this framework to include dependency injection, and in particular, as soon as I create a beans.xml file ( even an empty one ), I get all sorts of errors like:

SEVERE: Exception while loading the app : CDI deployment failure: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)]

I've tried using the new bean-discovery-mode attribute set to all or none in my beans.xml file but it makes no difference. Is this a bug in Glassfish or Jersey, or are they currently incompatible even though Glassfish includes all the jars involved, or am I doing something really silly?

Was it helpful?

Solution

It turns out that the error messages and beans.xml behaviour are red herrings. To help anyone with the same problem, this is what you need to do to use multipart mime inside a restful interface on glassfish.

Make sure the library is added only at compile time. If you use netbeans, this means adding multipart-mime-xxx.jar from the glassfish/modules directory as a library, but unclicking the 'package' button, so it is not included in the war package ( since it's already inside glassfish anyway.

If you are using maven, you achieve the same result by using a provided tag inside the dependency:

        <dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <scope>provided</scope>
        </dependency>

If you are using a restful template generated by netbeans as your starting point, you will have a file called application-config.java which has been generated for you. Add the line:

    resources.add(MultiPartFeature.class);

immediately above the line

     addRestResourceClasses(resources);

Now you should find that you can safely use the various annotations for multipart mime in jersey.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top