30

我有一个方法:

@POST
@Consumes("multipart/form-data")
@Produces( {"text/xml"})
public Response processForm(
    @FormDataParam("myparam") InputStream is,
    @FormDataParam("myparam") FormDataContentDisposition detail)

与 Jersey 1.x 一起工作得很好。

我正在升级到 2.0 m11。

现在我收到以下错误:

12/01/2013 11:15:04 AM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.0-m11 2012-12-21 12:34:15...
12/01/2013 11:15:04 AM org.glassfish.jersey.internal.Errors processErrors
SEVERE: The following errors and warnings have been detected:
WARNING: No injection source found for a parameter of type public javax.ws.rs.core.Response com.plutext.FileUpload.processForm(java.io.InputStream,org.glassfish
.jersey.media.multipart.FormDataContentDisposition) at index 0.

我发现 http://java.net/jira/browse/JERSEY-1413并提交http://java.net/projects/jersey/lists/commits/archive/2012-09/message/126这似乎相关,但它对我来说如何解决这个问题并不明显。

更新

我制作了一个 servlet,它在 org.glassfish.jersey.server.ApplicationHandler 初始化之前在 Tomcat 中运行:

public class Jersey2Init extends HttpServlet {

    private static final Logger jul = Logger.getLogger(Jersey2Init.class
        .getName());

    static {    
        System.out.println("\n\nrunning Jersey2Init\n\n");

        final ResourceConfig resourceConfig1 = new ResourceConfig(XFormService.class);
        resourceConfig1.registerInstances(new LoggingFilter(jul, true));
        resourceConfig1.register(MultiPartFeature.class);       

        final ResourceConfig resourceConfig2 = new ResourceConfig(AssembleService.class);
        resourceConfig2.registerInstances(new LoggingFilter(jul, true));
        resourceConfig2.register(MultiPartFeature.class);       
    }
}

它肯定首先运行:

INFO: Deploying web application archive C:\Java\apache-tomcat-7.0.29\webapps\Foo-Services.war


running Jersey2Init


18/01/2013 9:09:51 PM org.glassfish.jersey.server.ApplicationHandler initialize
INFO: Initiating Jersey application, version Jersey: 2.0-m11 2012-12-21 12:34:15...
18/01/2013 9:09:52 PM org.glassfish.jersey.internal.Errors processErrors
SEVERE: The following errors and warnings have been detected:

但我仍然得到同样的错误。

4

5 回答 5

44

您需要在您的应用程序上启用 MultiPart 功能。启用此功能会向 Jersey 2 应用程序注入必要的消息正文阅读器和编写器。以下是您注册它们的方法:

在服务器端(http-server):

final ResourceConfig resourceConfig = new ResourceConfig(MultiPartResource.class);
resourceConfig.register(MultiPartFeature.class);

在服务器端(servlet 部署):

import org.glassfish.jersey.filter.LoggingFilter;
import org.glassfish.jersey.media.multipart.MultiPartFeature;

import javax.ws.rs.core.Application;
import java.util.HashSet;
import java.util.Set;

public class MyApplication extends Application {
    @Override
    public Set<Class<?>> getClasses() {
        final Set<Class<?>> classes = new HashSet<Class<?>>();
        // register resources and features
        classes.add(MultiPartFeature.class);
        classes.add(MultiPartResource.class);
        classes.add(LoggingFilter.class);
        return classes;
    }
}

web.xml

<?xml version="1.0" encoding="UTF-8"?>
    <web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">
    <servlet>
        <servlet-name>Jersey Servlet</servlet-name>
        <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
        <init-param>
            <param-name>javax.ws.rs.Application</param-name>
            <param-value>com.aruld.jersey.multipart.MyApplication</param-value>
        </init-param>
        <load-on-startup>1</load-on-startup>
    </servlet>
    <servlet-mapping>
        <servlet-name>Jersey Servlet</servlet-name>
        <url-pattern>/*</url-pattern>
    </servlet-mapping>
</web-app>

在客户端:

final ClientConfig clientConfig = new ClientConfig();
clientConfig.register(MultiPartFeature.class);
Client client = ClientFactory.newClient(clientConfig);

我在这里的 Github 中整理了一个端到端的 Jersey 2 MultiPart 示例。

于 2013-01-18T06:47:44.710 回答
29

我试图使用 Jersey 2 进行文件上传。我想避免创建自定义 Application 或 ResourceConfig 类来启用 MultiPart。它没有很好的文档记录,但是如果你想添加 Multipart 功能,你所要做的就是将它添加到 web.xml 中的 Jersey servlet 配置中:

<init-param>
    <param-name>jersey.config.server.provider.classnames</param-name>
    <param-value>org.glassfish.jersey.filter.LoggingFilter;org.glassfish.jersey.media.multipart.MultiPartFeature</param-value>
</init-param>

如您所见,我还添加了一个日志过滤器。

于 2014-08-14T16:06:58.767 回答
3

并使用带注释的应用程序实例......

@ApplicationPath("restAPI")
public class ServiceApplication extends ResourceConfig {
    public ServiceApplication() {
        register(JAXBContextResolver.class);
        register(JacksonFeature.class);
        register(MultiPartFeature.class);
        registerInstances(new LoggingFilter(Logger.getLogger(ServiceApplication.class.getName()), true));
    }
}
于 2013-12-31T17:18:14.177 回答
1

经过很多努力,我发现你必须导入import org.glassfish.jersey.media.multipart.FormDataParam;而不是import javax.ws.rs.FormParam;这样你才能使用@FormDataParam

于 2018-08-01T05:02:42.873 回答
0

我使用的是 Jersey 2.7 并且遇到了同样的错误。它通过升级到 Jersey 2.9 自行解决

于 2014-07-01T14:07:15.423 回答