JsonMappingException:找不到非具体Map类型的反序列化器

时间:2016-09-07 15:05:11

标签: java serialization deserialization jersey-2.0 jersey-client

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

我得到了

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"}

然后我更改为此代码

    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>(){});

并得到例外:

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

其中:

@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 + '\'' +
                '}';
    }
}

我如何以不同方式反序列化?

更新

我试过了:

public class Version {

    public Map<String, String> versions;

    public String provider;

    // getters, setters, etc
}

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

但得到了同样的错误

1 个答案:

答案 0 :(得分:2)

我想说对象Version需要一个名为versions的{​​{1}}类型的字段和另一个名为Map<String, String>的类型为provider的字段,而您可能不需要String

GenericType

然后

public class Version {

    private Map<String, String> versions;

    private String provider;

    // getters, setters, etc
}

使用

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

输出:&#34; topic-test-publisher-1.0.16&#34;