我是网络服务的新手,我正在开发一个文档管理系统,并尝试使用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}']
项目库
所以,我找不到任何解决方案,是否有任何不同的方式上传文件与多个对象与一个休息呼叫。
请指导我。
答案 0 :(得分:2)
您使用的是错误的版本多部分依赖项。无论何时你在包中看到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
除非客户端能够为每个正文部分设置Content-Type
(某些客户端不能),否则您需要稍微调整一下该方法,以便将结果作为对象。例如
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