@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
Simplest way is to add support through web.xml file. 3. Write Jersey File Upload REST API Now look at actual REST API for file upload, which will receive and save the file. throw new WebApplicationException ("Error while uploading file. Please try again !!"); 5. Test file upload using HTML Form
To support multipart (file upload) in Jersey, you just need to include “ jersey-multipart.jar ” in Maven pom.xml file. 2. File Upload HTML Form Simple HTML form to select and upload a file. 3. Upload Service with Jersey In Jersey, use @FormDataParam to receive the uploaded file.
File Upload HTML Form Simple HTML form to select and upload a file. 3. Upload Service with Jersey In Jersey, use @FormDataParam to receive the uploaded file. To get the uploaded file name or header detail, match it to “ FormDataContentDisposition “. 4. Demo
Our upload method takes two parameters @FormDataParam. One of the @FormDataParam provides an InputStream to the file being uploaded and the other @FormDataParam provides content disposition header by using the FormDataContentDisposition object in the Jersey API. import java.io.FileOutputStream;
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
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With