1

我的环境是 JBOSS AS 5.1.0-GA、Struts 2.1.8 和 RESTEasy 1.2.GA

发布到我的服务时,我在从 @Consumes 注释调用的 Block getter 上得到一个 NPE。我不知道它可能是什么,任何帮助将不胜感激。

先感谢您。

城野

public class MyForm
{
    @FormParam("block")
    @PartType("application/octet-stream")
    private byte[] block;

    public MyForm(){}

    public byte[] getBlock()
    {
        return block;
    }

    public void setBlock(final byte[] block)
    {
        this.block =  block;
    }
}


@Path("/service")  
public class Service
{

    @Consumes("multipart/form-data")
    @POST
    public Response post(@MultipartForm MyForm form)
    {
        System.out.println(" recieved form " + form);
        ......
    }
}

例外

请注意,我删除了行号,因为它们与示例中的行号不对应。

java.lang.NullPointerException
    at MyForm.getBlock(MyForm.java:)
    at Service.postFileBlock(Service.java:)
    at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
    at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
    at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
    at java.lang.reflect.Method.invoke(Method.java:597)
    at org.jboss.resteasy.core.MethodInjectorImpl.invoke(MethodInjectorImpl.java:119)
    at org.jboss.resteasy.core.ResourceMethod.invokeOnTarget(ResourceMethod.java:211)
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:176)
    at org.jboss.resteasy.core.ResourceMethod.invoke(ResourceMethod.java:166)
    at org.jboss.resteasy.core.SynchronousDispatcher.getResponse(SynchronousDispatcher.java:359)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:336)
    at org.jboss.resteasy.core.SynchronousDispatcher.invoke(SynchronousDispatcher.java:103)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:114)
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.service(HttpServletDispatcher.java:70)
    at javax.servlet.http.HttpServlet.service(HttpServlet.java:717)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.struts2.dispatcher.ng.filter.StrutsExecuteFilter.doFilter(StrutsExecuteFilter.java:67)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.obtainContent(SiteMeshFilter.java:129)
    at com.opensymphony.sitemesh.webapp.SiteMeshFilter.doFilter(SiteMeshFilter.java:77)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.struts2.dispatcher.ng.filter.StrutsPrepareFilter.doFilter(StrutsPrepareFilter.java:82)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.jboss.web.tomcat.filters.ReplyHeaderFilter.doFilter(ReplyHeaderFilter.java:96)
    at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
    at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
    at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:235)
    at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:191)
    at org.jboss.web.tomcat.security.SecurityAssociationValve.invoke(SecurityAssociationValve.java:190)
    at org.jboss.web.tomcat.security.JaccContextValve.invoke(JaccContextValve.java:92)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.process(SecurityContextEstablishmentValve.java:126)
    at org.jboss.web.tomcat.security.SecurityContextEstablishmentValve.invoke(SecurityContextEstablishmentValve.java:70)
    at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:127)
    at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:102)
    at org.jboss.web.tomcat.service.jca.CachedConnectionValve.invoke(CachedConnectionValve.java:158)
    at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
    at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:330)
    at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:829)
    at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:598)
    at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
    at java.lang.Thread.run(Thread.java:637)

请注意:这是来自 resteasy 开发者邮件列表的交叉帖子。

4

2 回答 2

1

二进制数据是一条红鲱鱼,我在错误的地方放了注释,它们应该在 setter 方法而不是成员变量上。

public class MyForm
{
   private byte[] block;

   public MyForm(){}

   public byte[] getBlock()
   {
       return block;
   }

   @FormParam("block")
   @PartType("application/octet-stream")
   public void setBlock(final byte[] block)
   {
       this.block =  block;
   }
}
于 2009-12-09T23:10:34.070 回答
0

我对 RestEasy 不熟悉,但是您的堆栈表明您没有 MyForm 的实例......因为它看起来像是基于反射,我建议您查看您的配置(可能是某种类型的依赖注入?)以确保对象正在被实例化。

于 2009-12-09T12:08:20.347 回答