使用 .getClass() 时如何将参数传递给构造函数?

发布于 2024-12-16 02:32:58 字数 1028 浏览 6 评论 0原文

我有这行代码,它在该版本上工作:

...
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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

纵性 2024-12-23 02:32:58

Wrapper.classnew Wrapper().getClass()new Wrapper(theParam).getClass() 返回相同的值:包装器.类。如果您有合适的构造函数,即能够获取参数theParam的构造函数,那么所有这一切都可以实现。在您的情况下,类 Wrapper 没有接受 Class 类型参数的构造函数,因此它会抱怨这一点。

Wrapper.class and new Wrapper().getClass() and new Wrapper(theParam).getClass() return the same value: Wrapper.class. All this if you have sutable constructor, i.e., constructor that is able to get argument theParam. In your case class Wrapper does not have constructor that accepts argument of type Class, so it complains about this.

流年已逝 2024-12-23 02:32:58

我假设您需要的是指示杰克逊使用的包装器的通用类型。有几种方法可以做到这一点:

Wrapper<Model> value = objectMapper.readValue(source, new TypeReference<Wrapper<Model>>() { });
Wrapper<Model> value = objectMapper.readValue(source, objectMapper.getTypeFactory().constructParametricType(Wrapper.class, Model.class);

我不确定 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:

Wrapper<Model> value = objectMapper.readValue(source, new TypeReference<Wrapper<Model>>() { });
Wrapper<Model> value = objectMapper.readValue(source, objectMapper.getTypeFactory().constructParametricType(Wrapper.class, Model.class);

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);

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文