gpt4 book ai didi

java - Jersey Restful Web 服务 - 使用多个对象上传文件

转载 作者:行者123 更新时间:2023-12-02 02:26:52 25 4
gpt4 key购买 nike

我是 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}']

项目库是

enter image description here

所以,我找不到任何解决方案,是否有任何不同的方法可以通过一次休息调用上传包含多个对象的文件。

请指导我。

最佳答案

  1. 您使用了错误版本的多部分依赖项。任何时候您在包中看到 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

  2. 除非客户端能够为每个正文部分设置 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

关于java - Jersey Restful Web 服务 - 使用多个对象上传文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47673105/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com