Jersey restful 网络服务 - 具有多个对象的文件上传
Jersey restful web service - File upload with multiple object
我是 Web 服务的新手,我正在开发文档管理系统并尝试使用 Jersey restful Web 服务保存文件和多个对象。
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();
}
}
我的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>
曾经运行这个服务,我遇到了以下错误
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}']
项目库是
所以,我找不到任何解决方案,是否有任何不同的方法可以通过一个 rest 调用上传包含多个对象的文件。
请指导我。
您使用了错误的版本多部分依赖。任何时候你在包中看到 com.sun.jersey
,那是 Jersey 1.x,你不应该将它用于 2.x 项目。您将需要切换版本,然后注册 MultiPartFeature
。有关详细信息,请参阅 MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response
除非客户端能够为每个 body 部分设置 Content-Type
(有些客户端不能),否则您需要稍微调整一下方法,为了得到结果object。例如
public Response post(@FormDataParam("doc") FormDataBodyPart docpart) {
docpart.setMediaType(MediaType.APPLICATION_JSON_TYPE);
UserDocument doc = docpart.getValueAs(UserDocument.class);
}
对于InputStream
参数,您不需要这样做。有关详细信息,请参阅 File upload along with other object in Jersey restful web service
我是 Web 服务的新手,我正在开发文档管理系统并尝试使用 Jersey restful Web 服务保存文件和多个对象。
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();
}
}
我的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>
曾经运行这个服务,我遇到了以下错误
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}']
项目库是
所以,我找不到任何解决方案,是否有任何不同的方法可以通过一个 rest 调用上传包含多个对象的文件。
请指导我。
您使用了错误的版本多部分依赖。任何时候你在包中看到
com.sun.jersey
,那是 Jersey 1.x,你不应该将它用于 2.x 项目。您将需要切换版本,然后注册MultiPartFeature
。有关详细信息,请参阅 MULTIPART_FORM_DATA: No injection source found for a parameter of type public javax.ws.rs.core.Response除非客户端能够为每个 body 部分设置
Content-Type
(有些客户端不能),否则您需要稍微调整一下方法,为了得到结果object。例如public Response post(@FormDataParam("doc") FormDataBodyPart docpart) { docpart.setMediaType(MediaType.APPLICATION_JSON_TYPE); UserDocument doc = docpart.getValueAs(UserDocument.class); }
对于
InputStream
参数,您不需要这样做。有关详细信息,请参阅 File upload along with other object in Jersey restful web service