1

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>)'

Nuñito Calzada
  • 4,394
  • 47
  • 174
  • 301
  • 1
    Have you tried [`convertValue`](https://stackoverflow.com/questions/25024999/convert-jsonnode-into-object) instead? – Mr. Polywhirl Jul 17 '23 at 17:35
  • No serializer found for class java.io.ByteArrayInputStream and no properties discovered to create BeanSerializer (to avoid exception, disable SerializationFeature.FAIL_ON_EMPTY_BEANS) – Nuñito Calzada Jul 17 '23 at 17:57
  • 1
    I assume you are using Jackson ObjectMapper. I think you are confusing between Jackson JsonNode and com.mashape.unirest.http.JsonNode. Object mapper requires TreeNode, Jackson JsonNode implements TreeNode, com.mashape.unirest.http.JsonNode doesn't. Try converting the com.mashape.unirest.http.JsonNode to a Json string and then use ObjectMapper.readValue(string,class). Give it a shot. – Ironluca Jul 19 '23 at 19:48

1 Answers1

1

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.

VonC
  • 1,262,500
  • 529
  • 4,410
  • 5,250