I’m trying to map a json api response to an object and IntelliJ is complaining. It’s saying cannot resolve method readValue(java.lang.String, java.lang.Object[]);. I realize I’m not passing the correct parameter but I’ve tried responseClass.class and responseClass.getClass() with no luck.
Usage:
MyClass myClass = new MyClass();
myClass.setResponseClass(User.class);
Definition:
MyClass {
private Object responseClass;
public void setResponseClass(Object responseClass) {
this.responseClass = responseClass;
}
public Object getResponseClass() {
return responseClass;
}
public void getApiResponse() {
//some code here
ObjectMapper mapper = new com.MyApp.Utility.ObjectMapper();
//some code here
//I've tried responseClass.class and responseClass.getClass(), it didn't like either of them
mapper.readValue(response, responseClass);
//more code here
}
}
This is a really odd way of mapping JSON responses to object, using Jackson. I assume you just want to be able to map arbitrary classes using a utility class? Here’s an example of a much easier way to accomplish this. Note that this uses the
ObjectMapperclass that comes with your Jackson distro:Now, you could still map your responses from JSON using the class you posted, with some modifications:
And using it as such: