泽西岛:找不到媒体类型= multipart/form-data的MessageBodyReader [英] Jersey: MessageBodyReader not found for media type=multipart/form-data

查看:619
本文介绍了泽西岛:找不到媒体类型= multipart/form-data的MessageBodyReader的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在SO上找到了一些类似的问题,但是似乎没有一个问题可以解决我的特定问题,而且我无法自行找到解决方案.

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.

这是我遇到的错误:

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.

我正在通过如下所示的jQuery AJAX请求发送此请求:

I am sending this through a jQuery AJAX request that looks like this:

$('#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');
    });
});

这是我的Java端点:

And this is my Java endpoint:

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

我已经看到一些人通过更改终结点方法的参数来使用@FormDataParam而不是FormDataMultiPart感到幸运(如

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.

我的pom.xml具有以下依赖性:

My pom.xml has the following dependencies:

<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

<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>

我能挖掘的唯一另一件事是使用ResourceConfig注册MultiPartFeature.但是,我正在使用的项目没有任何应用程序类或任何扩展ResourceConfig的类(这是已部署到Tomcat的WAR,因此没有主类).

The only other thing I was able to dig up was to register the MultiPartFeature using a ResourceConfig; 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).

是否还需要进行其他配置?我为为什么这不起作用而感到困惑.

Is there any other configuration that needs to be done? I'm stumped as to why this is not working.

推荐答案

MultiPartFeature具有必需的读取器和写入器.但是您仍然需要注册该功能.如前所述,您经常会在Application/ResourceConfig子类中看到它的注册.但是在web.xml中,您可以简单地将其添加到要添加为提供程序的类列表中.您可以通过在servlet配置中添加<init-param>来实现此目的,即

The MultiPartFeature has the required reader and writer. But you still need to register the feature. As you've mentioned, you will often see it's registration in an Application/ResourceConfig subclass. But in a web.xml, you can simply add it to the list of classes to add as provider. You can do that by adding an <init-param> to the servlet configuration, i.e.

<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.

这篇关于泽西岛:找不到媒体类型= multipart/form-data的MessageBodyReader的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆