使用 .getClass() 时如何将参数传递给构造函数?
我有这行代码,它在该版本上工作:
...
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]
如何将参数发送到一个对象,我将获得它的值的类?
I have that line of code and it was working at that version:
...
Wrapper<Model> wrapped = restTemplate.getForObject(BASE_URL, Wrapper.class, map);
...
However I want to send parameter to constructor:
...
Wrapper<Model> wrapped = restTemplate.getForObject(BASE_URL, new Wrapper(Model.class).getClass(), map);
...
It throws me an exception:
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]
How can I send parameter to an object that I will get the class of value of it?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
Wrapper.class
和new Wrapper().getClass()
和new Wrapper(theParam).getClass()
返回相同的值:包装器.类
。如果您有合适的构造函数,即能够获取参数theParam
的构造函数,那么所有这一切都可以实现。在您的情况下,类Wrapper
没有接受Class
类型参数的构造函数,因此它会抱怨这一点。Wrapper.class
andnew Wrapper().getClass()
andnew Wrapper(theParam).getClass()
return the same value:Wrapper.class
. All this if you have sutable constructor, i.e., constructor that is able to get argumenttheParam
. In your case classWrapper
does not have constructor that accepts argument of typeClass
, so it complains about this.我假设您需要的是指示杰克逊使用的包装器的通用类型。有几种方法可以做到这一点:
我不确定 TypeReference 或 JavaType (它们是启用泛型的替代方案,用于传递 Class 实例(类型被删除,即没有泛型!))如何通过 Spring 框架,但我假设它应该是可能的。
或者,如果这不起作用,请尝试对 Wrapper 进行子类化 - 具体子类实际上将具有必要的信息:
public class ModelWrapper extends Wrapper { }
ModelWrapper 包裹 =restTemplate.getForObject(BASE_URL, ModelWrapper.class);
I assume what you need is to indicate generic type of Wrapper for Jackson to use. There are couple of ways to do this:
I am not sure how either TypeReference or JavaType (which are generics-enabled alternatives to passing Class instances (that are type-erased, i.e. no generics!)) through Spring framework, but I assume it should be possible.
Alternatively, if that can't be made to work, try sub-classing Wrapper -- concrete sub-class WILL actually have information necessary:
public class ModelWrapper extends Wrapper { }
ModelWrapper wrapped = restTemplate.getForObject(BASE_URL, ModelWrapper.class);