javaserializationdeserializationjersey-2.0jersey-client

JsonMappingException: Can not find a deserializer for non-concrete Map type


    String str = commonClient.authorizedRequestBuilder(commonClient.webTarget
            .path("/apps/get_current_version/default/"+appName+"/"+appName)
            .queryParam("object_type", "app"))
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get()
            .readEntity(String.class);

i get

str = {"versions": {"ap": "Not Set", "am": "topic-test-publisher-1.0.16", "il": "topic-test-publisher-1.0.16", "row": "topic-test-publisher-1.0.49"}, "provider": "gce"}

Then i have changed to this code

    Version version = commonClient.authorizedRequestBuilder(commonClient.webTarget
            .path("/apps/get_current_version/default/"+appName+"/"+appName)
            .queryParam("object_type", "app"))
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get(ClientResponse.class)
            .readEntity(new GenericType<Version>(){});

and got an exception:

Caused by: com.fasterxml.jackson.databind.JsonMappingException: Can not find a deserializer for non-concrete Map type [map type; class javax.ws.rs.core.MultivaluedMap, [simple type, class java.lang.String] -> [collection type; class java.util.List, contains [simple type, class java.lang.Object]]]
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:269)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:428)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:947)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:439)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:296)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findContextualValueDeserializer(DeserializationContext.java:428)
    at com.fasterxml.jackson.databind.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:947)
    at com.fasterxml.jackson.databind.deser.BeanDeserializerBase.resolve(BeanDeserializerBase.java:439)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:296)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCacheValueDeserializer(DeserializerCache.java:244)
    at com.fasterxml.jackson.databind.deser.DeserializerCache.findValueDeserializer(DeserializerCache.java:142)
    at com.fasterxml.jackson.databind.DeserializationContext.findRootValueDeserializer(DeserializationContext.java:461)
    at com.fasterxml.jackson.databind.ObjectReader._findRootDeserializer(ObjectReader.java:1749)
    at com.fasterxml.jackson.databind.ObjectReader._bind(ObjectReader.java:1465)
    at com.fasterxml.jackson.databind.ObjectReader.readValue(ObjectReader.java:912)
    at com.fasterxml.jackson.jaxrs.base.ProviderBase.readFrom(ProviderBase.java:810)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.invokeReadFrom(ReaderInterceptorExecutor.java:256)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor$TerminalReaderInterceptor.aroundReadFrom(ReaderInterceptorExecutor.java:235)
    at org.glassfish.jersey.message.internal.ReaderInterceptorExecutor.proceed(ReaderInterceptorExecutor.java:155)
    at org.glassfish.jersey.message.internal.MessageBodyFactory.readFrom(MessageBodyFactory.java:1085)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:874)
    at org.glassfish.jersey.message.internal.InboundMessageContext.readEntity(InboundMessageContext.java:808)
    at org.glassfish.jersey.client.ClientResponse.readEntity(ClientResponse.java:326)
    at org.glassfish.jersey.client.JerseyInvocation.translate(JerseyInvocation.java:803)
    ... 32 more
Caused by: java.lang.IllegalArgumentException: Can not find a deserializer for non-concrete Map type [map type; class javax.ws.rs.core.MultivaluedMap, [simple type, class java.lang.String] -> [collection type; class java.util.List, contains [simple type, class java.lang.Object]]]
    at com.fasterxml.jackson.databind.deser.BasicDeserializerFactory.createMapDeserializer(BasicDeserializerFactory.java:1158)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer2(DeserializerCache.java:380)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createDeserializer(DeserializerCache.java:352)
    at com.fasterxml.jackson.databind.deser.DeserializerCache._createAndCache2(DeserializerCache.java:264)
    ... 59 more

where:

@JsonIgnoreProperties(ignoreUnknown = true)
public class Version {
    @JsonProperty("am")
    public String am;
    @JsonProperty("row")
    public String row;

    @Override
    public String toString() {
        return "VersionDto{" +
                "am='" + am + '\'' +
                ", row='" + row + '\'' +
                '}';
    }
}

How can i deserialize it differently?

UPDATE

I have tried:

with

public class Version {

    public Map<String, String> versions;

    public String provider;

    // getters, setters, etc
}

and

    Version version = commonClient.authorizedRequestBuilder(commonClient.webTarget
            .path("/apps/get_current_version/gce/waze-prod/default/"+appName+"/"+appName)
            .queryParam("object_type", "app"))
            .accept(MediaType.APPLICATION_JSON_TYPE)
            .get(ClientResponse.class)
            .readEntity(Version.class);

but got the same error


Solution

  • I would say the object Version need to have a field named versions of type Map<String, String> and another one named provider of type String, and you probably don't need the GenericType thing.

    public class Version {
    
        private Map<String, String> versions;
    
        private String provider;
    
        // getters, setters, etc
    }
    

    then

    Version version = commonClient.authorizedRequestBuilder(commonClient.webTarget)
                // ...
                .readEntity(Version.class);
    version.getVersions().get("am"); // to get "am"
    

    Tested with

    import com.fasterxml.jackson.databind.ObjectMapper;
    import java.io.IOException;
    import java.util.Map;
    
    public class Test {
        public static void main(String[] args) throws IOException {
            String str = "{\"versions\": {\"ap\": \"Not Set\", \"am\": \"topic-test-publisher-1.0.16\", \"il\": \"topic-test-publisher-1.0.16\", \"row\": \"topic-test-publisher-1.0.49\"}, \"provider\": \"gce\"}";
            ObjectMapper objectMapper = new ObjectMapper();
            Version v = objectMapper.readValue(str, Version.class);
    
            System.out.println(v.versions.get("am"));
        }
    
        public static class Version {
            public Map<String, String> versions;
    
            public String provider;
        }
    }
    

    Output: "topic-test-publisher-1.0.16"