I have this piece of code:
HttpResponse<JsonNode> response = Unirest.post("https://json.islandia.com/v1/martorell")
.basicAuth("624823", "8f1addd21a09d6b95eaefa8d60p4c05")
.field("day", "28")
.asJson();
Root newJsonNode = new ObjectMapper().treeToValue(response.getBody(), Root.class);
but I have this error: Cannot resolve method 'treeToValue(JsonNode, Class<Root>)'
Considering
ObjectMapper#treeToValue()does exist, check your import at the begginning of your file: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:
So you are trying to serialize a
ByteArrayInputStreamobject, 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:
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 usingObjectMapper.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.