简体   繁体   English

Jersey:找不到媒体类型= multipart / form-data的MessageBodyReader

[英]Jersey: MessageBodyReader not found for media type=multipart/form-data

I have found a few questions like this on SO already but none of them seemed to address my particular problem, and I have been unable to find a solution on my own. 我已经在SO上发现了一些这样的问题但是它们似乎都没有解决我的特定问题,而且我一直无法找到解决方案。

Here is the error I'm getting: 这是我得到的错误:

Caused by: org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyReader not found for media type=multipart/form-data; boundary=----WebKitFormBoundaryHWk1XUaeu7pEiDth, type=class org.glassfish.jersey.media.multipart.FormDataMultiPart, genericType=class org.glassfish.jersey.media.multipart.FormDataMultiPart.

I am sending this through a jQuery AJAX request that looks like this: 我通过一个看起来像这样的jQuery AJAX请求发送它:

$('#upload-image-form').on('submit', function(e) {
    e.preventDefault();
    var data = new FormData(this);
    $.ajax({
        url: url,
        method: 'POST',
        contentType: false,
        processData: false,
        data: data,
    }).done(function(data) {
        console.log(data);
    }).fail(function(res, status) {
        onError(res, status, 'Image upload failed');
    });
});

And this is my Java endpoint: 这是我的Java端点:

@POST
@Path("/{userId}")
@Consumes("multipart/form-data")
public Response createGraphic(
   @PathParam("userId") int userId,
   FormDataMultiPart multiPartFormData) { ... }

I have seen a few people have luck with changing the parameter of the endpoint method to use @FormDataParam instead of FormDataMultiPart (as seen here ), but I cannot edit the Java class, so I must use it how it is above. 我见过几个人有改变端点方法的参数使用@FormDataParam而不是FormDataMultiPart(如图运气在这里 ),但我不能编辑Java类,所以它是如何上面我必须使用它。

My pom.xml has the following dependencies: 我的pom.xml具有以下依赖项:

<dependency>
    <groupId>org.jvnet</groupId>
    <artifactId>mimepull</artifactId>
    <version>1.6</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.core</groupId>
    <artifactId>jersey-server</artifactId>
    <version>2.12</version>
</dependency>
<dependency>
    <groupId>org.glassfish.jersey.media</groupId>
    <artifactId>jersey-media-multipart</artifactId>
    <version>2.12</version>
</dependency>

web.xml web.xml中

<servlet>
   <servlet-name>Jersey</servlet-name>
   <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
   <init-param>
      <param-name>jersey.config.server.provider.packages</param-name>
      <param-value>my.package</param-value>
   </init-param>
   <load-on-startup>5</load-on-startup>
</servlet>

<servlet-mapping>
   <servlet-name>Jersey</servlet-name>
   <url-pattern>/api/*</url-pattern>
</servlet-mapping>

The only other thing I was able to dig up was to register the MultiPartFeature using a ResourceConfig; 我能够挖掘的唯一另一件事是使用ResourceConfig注册MultiPartFeature; however, the project I'm working with does not have any Application classes or any class that extends ResourceConfig (it's a WAR that's deployed to Tomcat, so no main class). 但是,我正在使用的项目没有任何Application类或任何扩展ResourceConfig的类(它是部署到Tomcat的WAR,所以没有主类)。

Is there any other configuration that needs to be done? 是否还需要进行其他配置? I'm stumped as to why this is not working. 我很难过为什么这不起作用。

The MultiPartFeature has the required reader and writer. MultiPartFeature具有所需的读写器。 But you still need to register the feature. 但您仍需要注册该功能。 As you've mentioned, you will often see it's registration in an Application/ResourceConfig subclass. 正如您所提到的,您经常会在Application/ResourceConfig子类中看到它的注册。 But in a web.xml, you can simply add it to the list of classes to add as provider. 但是在web.xml中,您只需将其添加到要添加为提供程序的类列表中。 You can do that by adding an <init-param> to the servlet configuration, ie 您可以通过向servlet配置添加<init-param>来实现,即

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>
        org.glassfish.jersey.media.multipart.MultiPartFeature,
        some.other.Provider
    </param-value>
</init-param>

As you can see in the example, if you need to register any other providers/features, you comma-delimit the class names. 正如您在示例中所看到的,如果您需要注册任何其他提供者/功能,则可以用逗号分隔类名。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data - org.glassfish.jersey.message.internal.MessageBodyProviderNotFoundException: MessageBodyWriter not found for media type=multipart/form-data ClientHandlerException:找不到MIME媒体类型multipart / form-data - ClientHandlerException: MIME media type, multipart/form-data, was not found 在JerseyTest中找不到媒体类型= multipart / form-data的MessageBodyWriter - MessageBodyWriter not found for media type=multipart/form-data in JerseyTest 使用Jersey发送多个文件:找不到multipart / form-data的MessageBodyWriter - Sending multiple files with Jersey: MessageBodyWriter not found for multipart/form-data 如何解决找不到媒体类型= multipart / form-data错误的MessageBodyWriter - How to resolve MessageBodyWriter not found for media type=multipart/form-data error 泽西错误:找不到媒体类型= application / json的MessageBodyReader - Jersey Error: MessageBodyReader not found for media type=application/json 内容类型的com.sun.jersey.core.header.FormDataContentDisposition类:multipart / form-data; - class com.sun.jersey.core.header.FormDataContentDisposition of content type: multipart/form-data; 具有泽西依赖关系的Gradle jar会为media type = application / json找不到MessageBodyReader - Gradle jar with jersey dependencies produces MessageBodyReader not found for media type=application/json 如何自动将multipart / form-data输入映射到Jersey中的Bean - How to automatically map multipart/form-data input to a bean in Jersey 泽西岛2多部分/表单数据问题。 InputStream为空(可用= 0) - Jersey 2 multipart/form-data issue. InputStream is empty (available=0)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM