Reputation: 483
I'm new to web service and I'm developing a document management system and trying to save the file and multiple object using Jersey restful web service.
import java.awt.Image;
import java.io.InputStream;
import java.util.List;
import javax.imageio.ImageIO;
import javax.swing.ImageIcon;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import javax.ws.rs.Consumes;
import javax.ws.rs.GET;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.PathParam;
import javax.ws.rs.Produces;
import javax.ws.rs.core.MediaType;
import javax.ws.rs.core.Response;
import com.sasianet.dmsservice.data.dao.UserDocumentDao;
import com.sasianet.dmsservice.data.entity.UserDocument;
import com.sasianet.dmsservice.data.entity.UserDocumentAttachment;
import com.sun.jersey.core.header.FormDataContentDisposition;
import com.sun.jersey.multipart.FormDataParam;
@Path("/userDocument")
public class UserDocumentService {
@GET
@Produces(MediaType.APPLICATION_JSON)
public List<UserDocument> findAll(){
List<UserDocument> userDocuments = null;
try{
UserDocumentDao udo = new UserDocumentDao();
userDocuments = udo.findAll();
}catch(Exception e){
e.printStackTrace();
}
return userDocuments;
}
@POST
@Path("/post/test")
@Consumes({MediaType.MULTIPART_FORM_DATA})
public Response uploadFileWithData(
@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition cdh,
@FormDataParam("userDoc") UserDocument userDocument,
@FormDataParam("attachment") UserDocumentAttachment userDocumentAttachment) throws Exception{
Image img = ImageIO.read(fileInputStream);
JOptionPane.showMessageDialog(null, new JLabel(new ImageIcon(img)));
System.out.println(cdh.getName());
System.out.println(userDocument.getDescription());
System.out.println(userDocumentAttachment.getAttachmentName());
return Response.ok("Cool Tools!").build();
}
}
My web.xml
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
id="WebApp_ID" metadata-complete="true" version="3.0">
<display-name>DMSService</display-name>
<servlet>
<servlet-name>Jersey REST Service</servlet-name>
<servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
<init-param>
<param-name>jersey.config.server.provider.packages</param-name>
<param-value>com.sasianet.dmsservice, com.fasterxml.jackson.jaxrs.json,com.jersey.jaxb</param-value>
</init-param>
</servlet>
<servlet-mapping>
<servlet-name>Jersey REST Service</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<context-param>
<param-name>jersey.media.type.mappings</param-name>
<param-value>json : application/json, xml : application/xml</param-value>
</context-param>
</web-app>
Once run this service, I faced the following error
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.sasianet.dmsservice.service.UserDocumentService.uploadFileWithData(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition,com.sasianet.dmsservice.data.entity.UserDocument) throws java.lang.Exception at index 0.; source='ResourceMethod{httpMethod=POST, consumedTypes=[multipart/form-data], producedTypes=[], suspended=false, suspendTimeout=0, suspendTimeoutUnit=MILLISECONDS, invocable=Invocable{handler=ClassBasedMethodHandler{handlerClass=class com.sasianet.dmsservice.service.UserDocumentService, handlerConstructors=[org.glassfish.jersey.server.model.HandlerConstructor@53d69bbb]}, definitionMethod=public javax.ws.rs.core.Response com.sasianet.dmsservice.service.UserDocumentService.uploadFileWithData(java.io.InputStream,com.sun.jersey.core.header.FormDataContentDisposition,com.sasianet.dmsservice.data.entity.UserDocument) throws java.lang.Exception, parameters=[Parameter [type=class java.io.InputStream, source=file, defaultValue=null], Parameter [type=class com.sun.jersey.core.header.FormDataContentDisposition, source=file, defaultValue=null], Parameter [type=class com.sasianet.dmsservice.data.entity.UserDocument, source=emp, defaultValue=null]], responseType=class javax.ws.rs.core.Response}, nameBindings=[]}', [WARNING] A resource, Resource{"/file", 0 child resources, 0 resource methods, 0 sub-resource locator, 0 method handler classes, 0 method handler instances}, with path "/file" is empty. It has no resource (or sub resource) methods neither sub resource locators defined.; source='Resource{"/file", 0 child resources, 0 resource methods, 0 sub-resource locator, 0 method handler classes, 0 method handler instances}']
Project libraries are
So, I cant find any solution to this and is there any different way to upload file with multiple object with one rest call.
Please guide me on this.
Upvotes: 0
Views: 2684
Reputation: 208994
You're using the wrong version multipart dependency. Anytime you see com.sun.jersey
in the package, that is for Jersey 1.x, and you should not use it for a 2.x project. You will need to switch versions and then register the MultiPartFeature
. For more information, see MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response
Unless the client is able to set the Content-Type
for each body part (some clients can't), you will need to tweak the method a little bit, in order to get the result an object. For example
public Response post(@FormDataParam("doc") FormDataBodyPart docpart) {
docpart.setMediaType(MediaType.APPLICATION_JSON_TYPE);
UserDocument doc = docpart.getValueAs(UserDocument.class);
}
For the InputStream
parameter, you don't need to do this. For more detail, see File upload along with other object in Jersey restful web service
Upvotes: 3