Reputation: 2569
I'm currently trying to deserialize an API result, which looks like the following
[{"name":"MyName","value":"MyValue"},{"name":"MyName2","value":"MyValue2"}]
ArrayList<BasicNameValuePair> entities = JsonUtils.getObjectMapper()
.readValue(receivedData.toString(),
new TypeReference<ArrayList<BasicNameValuePair>>() {});
Then the following exceptions occurs
Exception mapping result.
No suitable constructor found for type...
Since this is an internal class from org.apache.http.message.BasicNameValuePair
, I can not annotate or edit it in any way. But I see (from other android projects) a lot of people using this class. Is there some way to get this working? Serializing to String
from BasicNameValuePair
works.
Upvotes: 1
Views: 813
Reputation: 279880
Jackson uses reflection to create an instance of your class. By default, it expects a no-arg constructor. The BasicNameValuePair
class does not have such a constructor. It has a constructor with two parameters, one for name
and one for value
.
Typically, if you had control of the class, you could annotate the constructor parameters with @JsonProperty
so that Jackson used that constructor instead of the no-arg constructor. Since you don't have control of the code, use Mixins.
Declare a class like so
public static abstract class BasicNameValuePairMixIn {
private BasicNameValuePairMixIn(@JsonProperty("name") String name, @JsonProperty("value") String value) { }
}
And configure your ObjectMapper
like so
// configuration for Jackson/fasterxml
objectMapper.addMixInAnnotations(BasicNameValuePair.class, BasicNameValuePairMixIn.class);
Jackson will now use the mixin as a template for your class.
If you are using the older version of Jackson, use the configuration as described here.
Upvotes: 3
Reputation: 77984
Try not to use reserve keywords in your parameter names and then try again. "name","value"
Upvotes: -2