简体   繁体   English

java.lang.RuntimeException:无法实例化@Form类。 没有no-arg构造函数

[英]java.lang.RuntimeException: Unable to instantiate @Form class. No no-arg constructor

I am writing a POJO as shown below. 我正在编写一个POJO ,如下所示。

public class EndpointParams implements Serializable {
    private static final long serialVersionUID = -5269761907708414499L;

    @QueryParam("pageNum") @DefaultValue("1") private int pageNum;
    @QueryParam("pageSize") @DefaultValue("25") private int pageSize;

    public EndpointParams (int pageNum, int pageSize) {
        this.pageNum = pageNum;
        this.pageSize = pageSize;
    }

    public int getPageNum() {
        return pageNum;
    }

    public void setPageNum(int pageNum) {
        this.pageNum = pageNum;
    }

    public int getPageSize() {
        return pageSize;
    }

    public void setPageSize(int pageSize) {
        this.pageSize = pageSize;
    }

    @Override
    public boolean equals(Object obj) {
        // logic here
    }

    @Override
    public int hashCode() {
        // logic here
    }
} 

When I deploy my application on JBOSS , I get the below error JBOSS上部署应用程序时,出现以下错误

10:12:30,321 ERROR [org.apache.catalina.core.ContainerBase.[jboss.web].[default-host].[/cq/event-bus]] (ServerService Thread Pool -- 114) JBWEB000289: Servlet com.something.sdlc.rest.application.MyApplication threw load() exception: java.lang.RuntimeException: Unable to instantiate @Form class. No no-arg constructor.
    at org.jboss.resteasy.core.FormInjector.<init>(FormInjector.java:32) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.InjectorFactoryImpl.createParameterExtractor(InjectorFactoryImpl.java:116) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.cdi.CdiInjectorFactory.createParameterExtractor(CdiInjectorFactory.java:51) [resteasy-cdi-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.MethodInjectorImpl.<init>(MethodInjectorImpl.java:42) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.InjectorFactoryImpl.createMethodInjector(InjectorFactoryImpl.java:76) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.cdi.CdiInjectorFactory.createMethodInjector(CdiInjectorFactory.java:57) [resteasy-cdi-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodInvoker.<init>(ResourceMethodInvoker.java:100) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.processMethod(ResourceMethodRegistry.java:280) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.register(ResourceMethodRegistry.java:251) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:221) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:193) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:179) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.addResourceFactory(ResourceMethodRegistry.java:156) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.core.ResourceMethodRegistry.addPerRequestResource(ResourceMethodRegistry.java:75) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.spi.ResteasyDeployment.registration(ResteasyDeployment.java:430) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.spi.ResteasyDeployment.start(ResteasyDeployment.java:241) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.ServletContainerDispatcher.init(ServletContainerDispatcher.java:112) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.jboss.resteasy.plugins.server.servlet.HttpServletDispatcher.init(HttpServletDispatcher.java:36) [resteasy-jaxrs-3.0.8.Final.jar:]
    at org.apache.catalina.core.StandardWrapper.loadServlet(StandardWrapper.java:1194) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardWrapper.load(StandardWrapper.java:1100) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardContext.loadOnStartup(StandardContext.java:3591) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.apache.catalina.core.StandardContext.start(StandardContext.java:3798) [jbossweb-7.4.8.Final-redhat-4.jar:7.4.8.Final-redhat-4]
    at org.jboss.as.web.deployment.WebDeploymentService.doStart(WebDeploymentService.java:161) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
    at org.jboss.as.web.deployment.WebDeploymentService.access$000(WebDeploymentService.java:59) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
    at org.jboss.as.web.deployment.WebDeploymentService$1.run(WebDeploymentService.java:94) [jboss-as-web-7.4.0.Final-redhat-19.jar:7.4.0.Final-redhat-19]
    at java.util.concurrent.Executors$RunnableAdapter.call(Executors.java:511) [rt.jar:1.8.0_77]
    at java.util.concurrent.FutureTask.run(FutureTask.java:266) [rt.jar:1.8.0_77]
    at java.util.concurrent.ThreadPoolExecutor.runWorker(ThreadPoolExecutor.java:1142) [rt.jar:1.8.0_77]
    at java.util.concurrent.ThreadPoolExecutor$Worker.run(ThreadPoolExecutor.java:617) [rt.jar:1.8.0_77]
    at java.lang.Thread.run(Thread.java:745) [rt.jar:1.8.0_77]
    at org.jboss.threads.JBossThread.run(JBossThread.java:122)

In order to solve this issue I have to include the no-args constructor as below. 为了解决此问题,我必须包括以下no-args构造函数。

public EndpointParams () {
}

Once I include this in my POJO , the deployment succeeds. 一旦将其包含在POJO ,部署就会成功。 What I want to know is, why do I need to explicitly include the no-args constructor even though I am not using it in my entire application ? 我想知道的是,即使我没有在整个应用程序中使用它,为什么也需要显式包含no-args构造函数? I have several other POJOs in my application where I have NOT included the no-arg constructor and everything works well. 我的应用程序中还有其他几个POJO,其中没有包含no-arg构造函数,并且一切正常。 I want to know under what circumstances, I should explicitly specify the no-arg constructor. 我想知道在什么情况下,我应该明确指定no-arg构造函数。

PS: I am using RestEasy, CDI, JBOSS. PS:我正在使用RestEasy,CDI,JBOSS。

This is just how it works for @Form / @BeanParam . 这就是@Form / @BeanParam RESTEasy will try to construct it using the no-arg, and then populate the properties. RESTEasy将尝试使用no-arg构造它,然后填充属性。 It doesn't know how to call your custom constructor. 它不知道如何调用自定义构造函数。 It doesn't go through the same DI system like other services (with CDI) or even deserialization (with say Jackson). 它不会像其他服务(使用CDI)一样经过相同的DI系统,甚至不会进行反序列化(比如说Jackson)。 With either of these two, custom constructor creation is allowed. 使用这两种方法之一,都可以创建自定义构造函数。 But these are different systems. 但是这些是不同的系统。

You delegate the creation of the objects to resteasy, which uses Jackson or Jettison. 您将对象的创建委托给resteasy,后者使用Jackson或Jettison。

I guess these libraries are using reflection to instantiate the new objects, something like that : 我猜这些库正在使用反射实例化新对象,就像这样:

EndpointParams object = Class.forName("EndpointParams").newInstance();

and then, it calls the setters with the parameters of your query (because of your annotation): 然后,它使用查询的参数调用设置器(因为有注释):

 object.setPageNum(xxx);
 object.setPageSize(yyy);

In fact, I do not know HOW resteasy would guess the values of pageNum and pageSize to use the constructor public EndpointParams (int pageNum, int pageSize) . 实际上,我不知道该如何轻松地猜测pageNum和pageSize的值以使用构造函数public EndpointParams (int pageNum, int pageSize) That's probably why a constructor with no parameter is mandatory for such kind of libraries. 这可能就是为什么没有参数的构造函数对于此类库是必需的。

And, as you defined a constructor with parameters, the default constructor (without parameters) is removed : The default constructor exists only if there is no other constructor defined in the class. 并且,当您定义带有参数的构造函数时,默认构造函数(不带参数)将被删除:仅当类中没有定义其他构造函数时,默认构造函数才存在。 In this case, you have to write explicitly the constructor without parameters, just as you did. 在这种情况下,您必须像以前一样显式地编写不带参数的构造函数。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 java.lang.RuntimeException:无法实例化活动…java.lang.InstantiationException:类没有零参数构造函数 - java.lang.RuntimeException: Unable to instantiate activity… java.lang.InstantiationException: class has no zero argument constructor java.lang.RuntimeException:无法实例化活动 - java.lang.RuntimeException: Unable to instantiate activity java.lang.RuntimeException:无法实例化活动 - java.lang.RuntimeException: Unable to instantiate activity java.lang.RuntimeException:无法实例化激活 - java.lang.RuntimeException: Unable to instantiate activit java.lang.runtimeexception无法实例化接收器 - java.lang.runtimeexception unable to instantiate receiver java.lang.RuntimeException:无法实例化活动 - java.lang.RuntimeException: Unable to instantiate the activity java.lang.RuntimeException:无法实例化活动 - java.lang.RuntimeException:Unable to instantiate activity java.lang.RuntimeException:无法启动活动ComponentInfo,无法实例化片段,找不到Fragment构造函数 - java.lang.RuntimeException: Unable to start activity ComponentInfo, Unable to instantiate fragment, could not find Fragment constructor java.lang.RuntimeException:无法实例化活动ComponentInfo {...}:java.lang.ClassNotFoundException:未找到类 - java.lang.RuntimeException: Unable to instantiate activity ComponentInfo{…}: java.lang.ClassNotFoundException: Didn't find class java.lang.RuntimeException:无法实例化活动ComponentInfo没有零参数构造函数 - java.lang.RuntimeException: Unable to instantiate activity ComponentInfo has no zero argument constructor
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM