- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我有需求写一个REST服务来支持文件上传;我正在使用 JBOSS/Resteasy/Jersey,如下所示:
@Path("document")
@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Produces(MediaType.TEXT_PLAIN)
public Response uploadFile(@FormDataParam("file") InputStream fileInputStream,
@FormDataParam("file") FormDataContentDisposition contentDispositionHeader,
@FormDataParam("selectedName") String selectedName,
@FormDataParam("name") String name,
@FormDataParam("notes") String notes) {
String documentId;
// upload the file
String filePath = SERVER_UPLOAD_LOCATION_FOLDER + contentDispositionHeader.getFileName();
try { fileUpload(fileInputStream, filePath);
}
catch (IOException e) { throw new ApplicationRuntimeException( "Unable to upload file: " + selectedName);
}
// create the document record
documentId = create(selectedName, name, notes);
return Response.ok("123456789").build();
}
为了测试服务,我编写了以下单元测试:
@Test
public void testCreateDocument() throws Exception {
Assert.assertNotNull(file);
Assert.assertTrue(file.canRead());
given()
.multiPart("notes", "my notes") .multiPart("selectedName", "selectedName")
.multiPart("name", "test.txt") .multiPart("file",file)
.contentType(MediaType.APPLICATION_OCTET_STREAM) .expect().body(equalTo("This is an uploaded test file."))
.when().post(DOCUMENT_URL);
}
单元测试失败。以下是单元测试收到的错误消息以及服务器日志
The server refused this request because the request entity is in a format not supported by the requested resource for the requested method.
另外,在发出上述单元测试时,会记录以下服务器日志:
09:26:40,340 WARN [org.jboss.resteasy.core.ExceptionHandler] (http-/127.0.0.1:8080-7) failed to execute: javax.ws.rs.NotSupportedException: Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form-data;boundary=J6UnCyDNsA50mzrPqDb2ctHPBb6fEpFJRF
at org.jboss.resteasy.core.interception.ServerReaderInterceptorContext.throwReaderNotFound(ServerReaderInterceptorContext.java:52) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:73) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:50) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.plugins.interceptors.encoding.GZIPDecodingInterceptor.aroundReadFrom(GZIPDecodingInterceptor.java:59) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:150) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:89) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:112) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:288) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:242) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:229) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.7.Final.jar:]
at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.7.Final.jar:]
at javax.servlet.http.HttpServlet.service(HttpServlet.java:847) [jboss-servlet-api_3.0_spec-1.0.2.Final-redhat-1.jar:1.0.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:295) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:214) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:149) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.jpa.interceptor.WebNonTxEmCloserValve.invoke(WebNonTxEmCloserValve.java:50) [jboss-as-jpa-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.jboss.as.web.security.SecurityContextAssociationValve.invoke(SecurityContextAssociationValve.java:169) [jboss-as-web-7.3.0.Final-redhat-14.jar:7.3.0.Final-redhat-14]
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:145) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:97) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:102) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:336) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:856) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:653) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:920) [jbossweb-7.2.2.Final-redhat-1.jar:7.2.2.Final-redhat-1]
at java.lang.Thread.run(Thread.java:744) [rt.jar:1.7.0_45]
我搜索了所有关于Could not find message body reader for type: class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form-data 错误但是找不到任何有用的东西。
最佳答案
我遇到了同样的问题,为了解决这个问题,我使用了 volkerbenders repository 中描述的解决方案.这是对我有用的解决方案:
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxrs</artifactId>
<version>3.0.11.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-multipart-provider</artifactId>
<version>3.0.11.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>org.jboss.resteasy</groupId>
<artifactId>resteasy-jaxb-provider</artifactId>
<version>3.0.11.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>commons-io</groupId>
<artifactId>commons-io</artifactId>
<version>2.4</version>
</dependency>
这是资源类:
import org.apache.commons.io.IOUtils;
import org.jboss.resteasy.plugins.providers.multipart.InputPart;
import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput;
import javax.ws.rs.Consumes;
import javax.ws.rs.POST;
import javax.ws.rs.Path;
import javax.ws.rs.core.MultivaluedMap;
import javax.ws.rs.core.Response;
import java.io.IOException;
import java.io.InputStream;
import java.util.List;
import java.util.Map;
@Path("/file")
public class FileResource {
public static final String UPLOADED_FILE_PARAMETER_NAME = "file";
@Path("/upload")
@POST
@Consumes("multipart/form-data")
public Response uploadFile(MultipartFormDataInput input) {
Map<String, List<InputPart>> uploadForm = input.getFormDataMap();
List<InputPart> inputParts = uploadForm.get(UPLOADED_FILE_PARAMETER_NAME);
for (InputPart inputPart : inputParts) {
MultivaluedMap<String, String> headers = inputPart.getHeaders();
try {
InputStream inputStream = inputPart.getBody(InputStream.class, null);
byte[] bytes = IOUtils.toByteArray(inputStream);
String filename = getFileName(headers);
//TODO: HERE you do whatever you want to do with the file
//...
} catch (IOException e) {
return Response.status(Response.Status.INTERNAL_SERVER_ERROR).entity(e.getMessage()).build();
}
}
return Response.status(Response.Status.OK).build();
}
private String getFileName(MultivaluedMap<String, String> headers) {
String[] contentDisposition = headers.getFirst("Content-Disposition").split(";");
for (String filename : contentDisposition) {
if ((filename.trim().startsWith("filename"))) {
String[] name = filename.split("=");
String finalFileName = sanitizeFilename(name[1]);
return finalFileName;
}
}
return "unknown";
}
private String sanitizeFilename(String s) {
return s.trim().replaceAll("\"", "");
}
}
关于file-upload - 如何防止 HTTP Rest Jboss resteasy Fileupload 因 'Could not find message body reader' 错误而失败?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23122310/
是否可以为所有意外错误创建全局异常处理程序。 因为不可能像这样制作所有可能的类: public class ExceptionHandler implements ExceptionMapper {.
我正在尝试同时运行 HTTPServer 和 REST 处理程序。一次只能工作一个,不能同时工作。我需要提供 html 页面和 api。 这是我的代码。 public class HttpSe
JBoss 告诉我们 http://docs.jboss.org/seam/3/rest/latest/reference/en-US/html/rest.client.html 要为 RestEAS
我们使用的是 Wildfly 10.1.0 和 Resteasy 3.1.1。在数百 rps 的吞吐量下,即使平均延迟非常低,我们也会看到随机的长请求。 我们正在查看 New Relic 在我们的应用
我有以下代码: var threadsWaiter = new CountDownLatch(customers.size()); for(var c: List customers) { se
考虑以下场景(使用 Rest easy 进行 Rest 实现): 客户端向我的 RestService 发送了请求。 我的休息服务更新了与请求相关的数据库。 最后我的休息服务发送了一些响应。 如果客户
我们在这里运行 RESTEasy 2.3.0.GA,我正在为 https://stackoverflow.com/questions/18219237/jax-rs-path-regex-fails-
我使用 JBoss (AS 7.1)、RestEasy 和 Jackson 开发 REST Api。 Web 服务返回一个“Account”对象,它是一个简单的 POJO,过去在 JSon 中序列化没
我需要使用 RESTEasy 设计 RESTful 服务。客户端可以使用任意数量的查询参数来调用此公共(public)服务。我的 REST 代码应该能够以某种方式读取这些查询参数。例如,如果我有图书搜
我需要创建 rest-easy 客户端,使用其他人创建的 RestService 的 de 接口(interface)...这很好,除了一件事...... 当我从 rest-easy 2.3.5.Fi
我在 resteasy-reactive 项目时收到消息。 (build-70) [io.quarkus.resteasy.common.deployment.ResteasyCommonProces
我写了一个我想测试的 Rest-Service。我想在不运行服务器的情况下运行 JUnit 测试。为此,我使用了 RestEasy 的服务器端模拟框架。 我的问题是,如何使用此框架在 Http-Bod
我正在开发 Resteasy。我将应用程序的 Maven 依赖项从 2.2.x 迁移到 3.0.x,突然间我发现大部分 API 都已弃用。所以这个迁移对我的代码和测试用例有影响,因为它在我的整个代码中
我在使用 RESTEASY 的休息应用程序上需要 CDI 功能。所以我跟着manual's instruction在我的应用程序上设置 resteasy-cdi 模块,它在 JBoss AS7 上运行
我将一个项目从 JBOSS 迁移到 Wildfly Server。我在 standalone.xml 中做了一些更改,一切正常,但是当我从 Postman 发送 JSON 请求时,出现以下异常: ER
我有一个使用restEasy编写的restful服务并部署到JBoss。我有一个 web.xml 被玷污为: Web Application org.restea
我有一个简单的 Hello World 示例 JAX-RS 项目。真的很简单也很愚蠢。只是最小的配置,我打算在未来增强,想象一下这样的事情:https://robferguson.org/blog/2
As it currently stands, this question is not a good fit for our Q&A format. We expect answers to be
我编写了一些异常映射器来捕获和处理内置的简单异常,如 NotFoundException、MethodNotAllowedException 等,示例代码如下所示: @Provider public
嗨,我正在尝试使用多部分表单上传多个文件 我使用这个,但我得到错误的请求状态,我如何上传多个文件? public class AttachmentBody { @FormParam("file
我是一名优秀的程序员,十分优秀!