RESTEASY003200:找不到类型的邮件正文 reader:class org.glassfish.jersey.media.multipart.FormDataMultiPart

RESTEASY003200: Could not find message body reader for type: class org.glassfish.jersey.media.multipart.FormDataMultiPart

我想上传图片并向我的 java 发送一些参数,其余的 service.I 已将 jersey-media-multipart 添加到我的 pom.xml 并且我将必要的配置设置为 ApplicationConfig class。我在应用程序中使用 Wildfly 11 server.But 我不断收到此异常。

11:29:36,199 ERROR [org.jboss.resteasy.resteasy_jaxrs.i18n] (default task-35) RESTEASY002010: Failed to execute: javax.ws.rs.NotSupportedException: RESTEASY003200: Could not find message body reader for type: class org.glassfish.jersey.media.multipart.FormDataMultiPart of content type: multipart/form-data;boundary=--------------------------291101341234694996301314
    at org.jboss.resteasy.core.interception.ServerReaderInterceptorContext.throwReaderNotFound(ServerReaderInterceptorContext.java:53)
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.getReader(AbstractReaderInterceptorContext.java:80)
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:53)
    at org.jboss.resteasy.security.doseta.DigitalVerificationInterceptor.aroundReadFrom(DigitalVerificationInterceptor.java:36)
    at org.jboss.resteasy.core.interception.AbstractReaderInterceptorContext.proceed(AbstractReaderInterceptorContext.java:59)
    at org.jboss.resteasy.core.MessageBodyParameterInjector.inject(MessageBodyParameterInjector.java:151)
    at org.jboss.resteasy.core.MethodInjectorImpl.injectArguments(MethodInjectorImpl.java:92)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:115)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:295)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:249)
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:236)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:406)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:213)
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:228)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790)
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:129)
    at com.ocpsoft.pretty.PrettyFilter.doFilter(PrettyFilter.java:145)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at com.mepsan.outra.global.LoginFilter.doFilter(LoginFilter.java:41)
    at io.undertow.servlet.core.ManagedFilter.doFilter(ManagedFilter.java:61)
    at io.undertow.servlet.handlers.FilterHandler$FilterChainImpl.doFilter(FilterHandler.java:131)
    at io.undertow.servlet.handlers.FilterHandler.handleRequest(FilterHandler.java:84)
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:62)
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36)
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:131)
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:57)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:46)
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:64)
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:60)
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:77)
    at io.undertow.security.handlers.NotificationReceiverHandler.handleRequest(NotificationReceiverHandler.java:50)
    at io.undertow.security.handlers.AbstractSecurityContextAssociationHandler.handleRequest(AbstractSecurityContextAssociationHandler.java:43)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at org.wildfly.extension.undertow.deployment.GlobalRequestControllerHandler.handleRequest(GlobalRequestControllerHandler.java:68)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:43)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:292)
    at io.undertow.servlet.handlers.ServletInitialHandler.access0(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:138)
    at io.undertow.servlet.handlers.ServletInitialHandler.call(ServletInitialHandler.java:135)
    at io.undertow.servlet.core.ServletRequestContextThreadSetupAction.call(ServletRequestContextThreadSetupAction.java:48)
    at io.undertow.servlet.core.ContextClassLoaderSetupAction.call(ContextClassLoaderSetupAction.java:43)
    at org.wildfly.extension.undertow.security.SecurityContextThreadSetupAction.lambda$create[=13=](SecurityContextThreadSetupAction.java:105)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1508)
    at org.wildfly.extension.undertow.deployment.UndertowDeploymentInfoService$UndertowThreadSetupAction.lambda$create[=13=](UndertowDeploymentInfoService.java:1508)
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:272)
    at io.undertow.servlet.handlers.ServletInitialHandler.access[=13=]0(ServletInitialHandler.java:81)
    at io.undertow.servlet.handlers.ServletInitialHandler.handleRequest(ServletInitialHandler.java:104)
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:326)
    at io.undertow.server.HttpServerExchange.run(HttpServerExchange.java:812)
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142)
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617)
    at java.lang.Thread.run(Thread.java:745)

ApplicationConfig.java

@javax.ws.rs.ApplicationPath("resources")
public class ApplicationConfig extends Application {

    .....

    @Override
    public Map<String, Object> getProperties() {
        Map<String, Object> props = new HashMap<>();
        props.put("jersey.config.server.provider.classnames",
                "org.glassfish.jersey.media.multipart.MultiPartFeature");
        return props;
    }
}

移动源.java

@Path("mobile")
public class MobileSource {

    @POST
    @Path("profile/upload")
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response execute(FormDataMultiPart multi) {

    }
}

pom.xml

<dependency>
        <groupId>org.glassfish.jersey.media</groupId>
        <artifactId>jersey-media-multipart</artifactId>
        <version>2.19</version>
 </dependency>

另一种方式

@POST
@Path("profile/upload")
@Consumes(MediaType.MULTIPART_FORM_DATA)
public Response execute(@FormDataParam("image") InputStream imgstream,
        @FormDataParam("data") String s) {
    //multi.getF
    try {
        System.out.println("S " + s);
        byte[] imgdata = inputStreamToByte(imgstream);
        //System.out.println("DATA STR " + data);
        //System.out.println("THE PARAM " + mstr);
        System.out.println("THE PARAM S " + imgdata.length);

    } catch (IOException ex) {
        Logger.getLogger(MobileSource.class.getName()).log(Level.SEVERE, null, ex);
    }

    return Response.ok().build();
}

当我使用这种方式处理请求时,我只是在日志中看到这个模糊的结果。

13:07:09,911 INFO  [stdout] (default task-41) S ----------------------------336065642279870055586849

13:07:09,911 INFO  [stdout] (default task-41) Content-Disposition: form-data; name="data"

13:07:09,911 INFO  [stdout] (default task-41) 

13:07:09,911 INFO  [stdout] (default task-41) test

13:07:09,911 INFO  [stdout] (default task-41) ----------------------------336065642279870055586849

13:07:09,911 INFO  [stdout] (default task-41) Content-Disposition: form-data; name="image"; filename="eagle.jpg"

13:07:09,911 INFO  [stdout] (default task-41) Content-Type: image/jpeg

13:07:09,911 INFO  [stdout] (default task-41) 

13:07:09,911 INFO  [stdout] (default task-41) ????.....

    abstruse continue....

13:07:16,804 INFO  [stdout] (default task-41) ----------------------------336065642279870055586849--

13:07:16,804 INFO  [stdout] (default task-41) 

13:07:16,804 INFO  [stdout] (default task-41) THE PARAM S 0

    FINISH

对于面对相同的人issue.This花了我2天的时间。我们需要用到jBoss RESTEasy的Multipart support 因为我们的应用服务器是JBOSS(最近才学的,顺便说一句,我用了很多Apache 3rd part 库而不使用 Apache Tomcat :p )。无论如何,我们将我们的多部分提供程序依赖项添加到我们的 pom.xml

<dependency>
    <groupId>org.jboss.resteasy</groupId>
    <artifactId>resteasy-multipart-provider</artifactId>
    <version>3.1.4.Final</version>
    <scope>provided</scope>
</dependency>

我们对 ApplicationConfig class 不做任何处理。这是休息部分

@POST
@Consumes(MediaType.MULTIPART_FORM_DATA)
@Path("upload")
public Response up1(MultipartFormDataInput input) {
        List<InputPart> jsonpart = input.getFormDataMap().get("jsondata");
        List<InputPart> imgpart = input.getFormDataMap().get("image");
        InputStream is2 = input.getFormDataPart("image", InputStream.class,null);
        String jsonStr = input.getFormDataPart("jsondata", String.class,null);
}

就这些了。