Home > Software engineering >  Java Jackson `readValue()` arguments not applicable
Java Jackson `readValue()` arguments not applicable

Time:01-05

I ran into an issue when trying to use readValue() of Jackson. I'm trying to get some value from my response. But I get "The method readValue(JsonParser, Class<T>) in the type ObjectMapper is not applicable for the arguments (HttpResponse<String>, new TypeReference<Map<String,Object>>(){})" Error. I tried to look up this error and couldn't find a solution.

I imported the below:

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.core.util.JsonParserSequence;
import com.fasterxml.jackson.databind.ObjectMapper;

This is my code:


HttpRequest urlAnalysisRequest = HttpRequest.newBuilder()
                    .uri(URI.create("https://www.virustotal.com/api/v3/analyses/....(I put the id here)"))
                    .header("Accept", "application/json")
                    .header("x-apikey", "....(I put api key here)")
                    .method("GET", HttpRequest.BodyPublishers.noBody())
                    .build();
                HttpResponse<String> urlAnalysisResponse;
                try {
                    urlAnalysisResponse = HttpClient.newHttpClient().send(urlAnalysisRequest, HttpResponse.BodyHandlers.ofString());
                    ObjectMapper objectMapper = new ObjectMapper();
                    
                    //**Didn't work:** List<String> listStats = ObjectMapper.readValue(urlAnalysisResponse, new TypeReference<List<String>>(){});
                    //Also didn't work:  Map<String, Object> map = objectMapper.readValue(urlAnalysisResponse, new TypeReference<Map<String,Object>>(){});
                   //Also didn't work:  String resp = objectMapper.writeValueAsString(urlAnalysisResponse);
                    System.out.println(resp);

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

This is my response. I'm trying to get "stats":


{
    "meta": {
        "url_info": {
            "url": "http://www.facebook.com/",
            "id": "114fb86b9b4e868f8bac2249eb5c444b545f0240c3dadd23312a0bc1622b5488"
        }
    },
    "data": {
        "attributes": {
            "date": 1641238171,
            "status": "completed",
            "stats": {
                "harmless": 84,
                "malicious": 0,
                "suspicious": 0,
                "undetected": 9,
                "timeout": 0
            },
....

And I have these dependencies:

Jackson-core-2.13.0.jar
jackson-annotations-2.13.0.jar
jackson-databind-2.13.0.jar

CodePudding user response:

Because you are interested just to the stats part of your json file you can read the JsonNode stats with the JsonNode#at method and subsequently convert it to Map<String,Integer> with the ObjectMapper#treeToValue method :

ObjectMapper objectMapper = new ObjectMapper();
JsonNode tree = mapper.readTree(json);
JsonNode stats = tree.at("/data/attributes/stats");
//map will be {harmless=84, malicious=0, suspicious=0, undetected=9, timeout=0}
Map<String, Integer> map = mapper.treeToValue(stats, Map.class);

CodePudding user response:

seems like you are trying to desriailize the httpResponse instead of the body

try this

HttpRequest urlAnalysisRequest = HttpRequest.newBuilder()
                    .uri(URI.create("https://www.virustotal.com/api/v3/analyses/....(I put the id here)"))
                    .header("Accept", "application/json")
                    .header("x-apikey", "....(I put api key here)")
                    .method("GET", HttpRequest.BodyPublishers.noBody())
                    .build();
                HttpResponse<String> urlAnalysisResponse;
                try {
                    urlAnalysisResponse = HttpClient.newHttpClient().send(urlAnalysisRequest, HttpResponse.BodyHandlers.ofString());
                    ObjectMapper objectMapper = new ObjectMapper();
                    
                     objectMapper.readValue(urlAnalysisResponse.body(), new TypeReference<Map<String,Object>>(){}); 

                } catch (IOException e) {
                    e.printStackTrace();
                } catch (InterruptedException e) {
                    e.printStackTrace();
                }

That should work but I suggest to use an implicit object instead of map for deserialization.

  •  Tags:  
  • Related