我有这行代码,它在该版本上工作:
...
Wrapper<Model> wrapped = restTemplate.getForObject(BASE_URL, Wrapper.class, map);
...
但是我想将参数发送给构造函数:
...
Wrapper<Model> wrapped = restTemplate.getForObject(BASE_URL, new Wrapper(Model.class).getClass(), map);
...
它抛出了一个异常:
org.springframework.web.client.ResourceAccessException: I/O error: No suitable constructor found for type [simple type, class a.b.c.d.model.Wrapper]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.commons.httpclient.AutoCloseInputStream@ef9e8eb; line: 1, column: 3]; nested exception is org.codehaus.jackson.map.JsonMappingException: No suitable constructor found for type [simple type, class a.b.c.d.model.Wrapper]: can not instantiate from JSON object (need to add/enable type information?)
at [Source: org.apache.commons.httpclient.AutoCloseInputStream@ef9e8eb; line: 1, column: 3]
如何向对象发送参数以获取其值的类别?
最佳答案
Wrapper.class
和 new Wrapper().getClass()
和 new Wrapper(theParam).getClass()
返回相同的值:Wrapper.class
。如果您有合适的构造函数,即能够获取参数 theParam
的构造函数,那么所有这一切都可以实现。在您的情况下,类 Wrapper
没有接受 Class
类型参数的构造函数,因此它会提示这一点。
关于java - 使用 .getClass() 时如何将参数传递给构造函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8133969/