javareflectionjacksonspring-remoting

How to pass parameter to constructor when using .getClass()?


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?


Solution

  • 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.