Resteasy:解除多部分请求时的低级别异常

时间:2014-08-10 10:39:38

标签: java json jaxb resteasy multipartform-data

我有一个Rest-Controller处理multipart / form-data请求。每个请求都包含一些需要解析的json格式的指令。

@Path("/media")
public class MediaController extends AbstractResourceController {
    private static final Logger logger = LoggerFactory.getLogger(MediaController.class);
    @POST
    @Consumes(MediaType.MULTIPART_FORM_DATA)
    public Response uploadMedia (MultipartFormDataInput input)
            throws IOException {
        input.getFormDataPart("JSON", Medium.class, null);
        return Response.ok().build();
    }
}

getFormDataPart调用会抛出IllegalArgumentException,但不提供太多信息。它只是说:"无法识别的类型:[null]"。

完整的堆栈跟踪如下所示:

10:55:21,691 ERROR [io.undertow.request] (default task-19) UT005023: Exception handling request to /testapp/api/media: org.jboss.resteasy.spi.UnhandledException: java.lang.IllegalArgumentException: Unrecognized Type: [null]
    at org.jboss.resteasy.core.ExceptionHandler.handleApplicationException(ExceptionHandler.java:76) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ExceptionHandler.handleException(ExceptionHandler.java:212) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.writeException(SynchronousDispatcher.java:149) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:372) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:179) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.service(ServletContainerDispatcher.java:220) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:56) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:51) [resteasy-jaxrs-3.0.8.Final.jar:]
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:790) [jboss-servlet-api_3.1_spec-1.0.0.Final.jar:1.0.0.Final]
    at io.undertow.servlet.handlers.ServletHandler.handleRequest(ServletHandler.java:85) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.security.ServletSecurityRoleHandler.handleRequest(ServletSecurityRoleHandler.java:61) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.ServletDispatchingHandler.handleRequest(ServletDispatchingHandler.java:36) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at org.wildfly.extension.undertow.security.SecurityContextAssociationHandler.handleRequest(SecurityContextAssociationHandler.java:78)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.security.SSLInformationAssociationHandler.handleRequest(SSLInformationAssociationHandler.java:113) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.security.ServletAuthenticationCallHandler.handleRequest(ServletAuthenticationCallHandler.java:56) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.security.handlers.AbstractConfidentialityHandler.handleRequest(AbstractConfidentialityHandler.java:45) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.security.ServletConfidentialityConstraintHandler.handleRequest(ServletConfidentialityConstraintHandler.java:61) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.security.handlers.AuthenticationMechanismsHandler.handleRequest(AuthenticationMechanismsHandler.java:58) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.security.CachedAuthenticatedSessionHandler.handleRequest(CachedAuthenticatedSessionHandler.java:70) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.security.handlers.SecurityInitialHandler.handleRequest(SecurityInitialHandler.java:76) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at org.wildfly.extension.undertow.security.jacc.JACCContextIdHandler.handleRequest(JACCContextIdHandler.java:61)
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.server.handlers.PredicateHandler.handleRequest(PredicateHandler.java:25) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.handleFirstRequest(ServletInitialHandler.java:240) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.dispatchRequest(ServletInitialHandler.java:227) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler.access$000(ServletInitialHandler.java:73) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.servlet.handlers.ServletInitialHandler$1.handleRequest(ServletInitialHandler.java:146) [undertow-servlet-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.server.Connectors.executeRootHandler(Connectors.java:168) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at io.undertow.server.HttpServerExchange$1.run(HttpServerExchange.java:727) [undertow-core-1.0.10.Final.jar:1.0.10.Final]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_20-ea]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_20-ea]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_20-ea]
Caused by: java.lang.IllegalArgumentException: Unrecognized Type: [null]
    at com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:390) [jackson-databind-2.3.2.jar:2.3.2]
    at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:338) [jackson-databind-2.3.2.jar:2.3.2]
    at com.fasterxml.jackson.databind.ObjectReader.withType(ObjectReader.java:454) [jackson-databind-2.3.2.jar:2.3.2]
    at org.jboss.resteasy.plugins.providers.jackson.ResteasyJackson2Provider.readFrom(ResteasyJackson2Provider.java:120) [resteasy-jackson2-provider-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.providers.multipart.MultipartInputImpl$PartImpl.getBody(MultipartInputImpl.java:290) [resteasy-multipart-provider-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInputImpl.getFormDataPart(MultipartFormDataInputImpl.java:48) [resteasy-multipart-provider-3.0.8.Final.jar:]
    at com.mycompany.testapp.rest.media.MediaController.uploadMedia(MediaController.java:24) [classes:]
    at com.mycompany.testapp.rest.media.MediaController$Proxy$_$$_WeldClientProxy.uploadMedia(Unknown Source) [classes:]
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method) [rt.jar:1.8.0_20-ea]
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:62) [rt.jar:1.8.0_20-ea]
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:43) [rt.jar:1.8.0_20-ea]
    at java.lang.reflect.Method.invoke(Method.java:483) [rt.jar:1.8.0_20-ea]
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:137) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodInvoker.invokeOnTarget(ResourceMethodInvoker.java:296) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:250) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodInvoker.invoke(ResourceMethodInvoker.java:237) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:356) [resteasy-jaxrs-3.0.8.Final.jar:]
    ... 30 more

我已经检查过一部分" JSON"存在。提交的数据非常简单:

{"title":"Look at my horse","description":"Look at my horse, my horse is amazing."}

相应的JAXB注释POJO是直截了当的。没什么特别的:

@XMLRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class Medium {
    public static final String KEY_TITLE = "title";
    public static final String KEY_DESCRIPTION = "description";

    @NotNull
    @XmlElement(name = KEY_TITLE)
    private String title;

    @XmlElement(name = KEY_DESCRIPTION)
    private String description;

    // Getters & Setters ....

}

真正让我感到困惑的是resteasy documentation它说:

  

每个部分都有一组与之关联的标题您可以通过调用其中一个getBody()方法来解组该部分。 Type genericType参数可以为null,但必须设置Class类型参数。 Resteasy将根据部件的媒体类型以及您传入的类型信息找到MessageBodyReader。

该部分指定" application / json"作为mediaType所以不应该有问题...

我在Wildfly 8中运行所有内容。

也许有人知道这是什么。从那个例外中我无法理解......

提前致谢!

1 个答案:

答案 0 :(得分:0)

我认为这样做的原因是在行中将null作为Type参数传递: -

"input.getFormDataPart("JSON", Medium.class, null)".

execption cause表明它正在尝试实例化Type类并遇到异常

引起:java.lang.IllegalArgumentException:无法识别的类型:[null]     at com.fasterxml.jackson.databind.type.TypeFactory._constructType(TypeFactory.java:390)[jackson-databind-2.3.2.jar:2.3.2]     at com.fasterxml.jackson.databind.type.TypeFactory.constructType(TypeFactory.java:338)[jackson-databind-2.3.2.jar:2.3.2]     在com.fasterxml.jackson.databind.ObjectReader.withType(ObjectReader.java:454)[jackson-databind-2.3.2.jar:2.3.2]

您应该传递有效的类型