Considering ObjectMapper#treeToValue()
does exist, check your import at the begginning of your file:
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
If you do not see those imports, that could explain the error message.
Make sure that the Jackson libraries are properly added to your project's classpath. You can add the following dependencies in your Maven's pom.xml:
<dependencies>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.12.1</version>
</dependency>
<dependency>
<groupId>com.mashape.unirest</groupId>
<artifactId>unirest-java</artifactId>
<version>1.4.9</version>
</dependency>
</dependencies>
If I try using convertValue
instead, I get:
No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, > disable SerializationFeature.FAIL_ON_EMPTY_BEANS)
So you are trying to serialize a ByteArrayInputStream
object, which Jackson does not know how to serialize by default.
HttpResponse.getBody()
might not be returning a JsonNode type: instead, it could be returning an InputStream which needs to be manually converted to JsonNode before you can utilize Jackson's conversion feature.
For instance:
HttpResponse<InputStream> response = Unirest.post("https://json.islandia.com/v1/martorell")
.basicAuth("624823", "8f1addd21a09d6b95eaefa8d60p4c05")
.field("day", "28")
.asObject(InputStream.class);
ObjectMapper mapper = new ObjectMapper();
JsonNode jsonNode = mapper.readTree(response.getBody());
Root newJsonNode = mapper.treeToValue(jsonNode, Root.class);
Here, the body of the HTTP response, which is an InputStream, is first converted to a JsonNode object using ObjectMapper.readTree()
. Then, the JsonNode is converted to a Root object using ObjectMapper.treeToValue()
. The Root class should be a POJO that matches the structure of the JSON you are working with. If it does not, you might run into more errors.