泽西2注射源为multipart formdata

时间:2013-01-12 00:27:48

标签: java jersey tomcat7 jax-rs multipartform-data

我有一个方法:

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

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

5 个答案:

答案 0 :(得分:40)

您需要在应用程序上启用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 here中整理了一个端到端的Jersey 2 MultiPart示例。

答案 1 :(得分:27)

我试图使用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>

如您所见,我还添加了一个loggingfilter。

答案 2 :(得分: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));
    }
}

答案 3 :(得分:0)

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

答案 4 :(得分:0)

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