gpt4 book ai didi

java - 找不到非具体 Map 类型的反序列化器 [map type;类 javax.ws.rs.core.MultivaluedMap

转载 作者:行者123 更新时间:2023-12-05 01:03:08 26 4
gpt4 key购买 nike

我在使用 org.codehaus.jackson.map.ObjectMapper 进行反序列化时遇到以下问题,该问题不适用于以下类。我不确定我正在使用的 MultivaluedMap 出了什么问题。

 public class ClassD
{
private ClassA objA;
private ClassB objB;
private final ClassC objC;
private MultivaluedMap<String, String> headerMap;
}

public static void main(String[] args) {
String fileName = "someFilePath";
mockCollection = fromJSON(new TypeReference<Collection<ClassD>>() {}, new String(Files.readAllBytes(Paths.get(fileName)), StandardCharsets.UTF_8));
}

public static <T> T fromJSON(final TypeReference<T> type, final String jsonPacket) throws JsonParseException, JsonMappingException, IOException {
T data = null;
ObjectMapper om = new ObjectMapper();
om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
data = om.readValue(jsonPacket, type);
return data;
}


Exception :
org.codehaus.jackson.map.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.String]]]
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:315)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159)
at org.codehaus.jackson.map.deser.std.StdDeserializer.findDeserializer(StdDeserializer.java:620)
at org.codehaus.jackson.map.deser.BeanDeserializer.resolve(BeanDeserializer.java:379)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._resolveDeserializer(StdDeserializerProvider.java:407)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:352)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159)
at org.codehaus.jackson.map.deser.BasicDeserializerFactory.createCollectionDeserializer(BasicDeserializerFactory.java:284)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createDeserializer(StdDeserializerProvider.java:389)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCache2(StdDeserializerProvider.java:310)
at org.codehaus.jackson.map.deser.StdDeserializerProvider._createAndCacheValueDeserializer(StdDeserializerProvider.java:290)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findValueDeserializer(StdDeserializerProvider.java:159)
at org.codehaus.jackson.map.deser.StdDeserializerProvider.findTypedValueDeserializer(StdDeserializerProvider.java:180)
at org.codehaus.jackson.map.ObjectMapper._findRootDeserializer(ObjectMapper.java:2829)
at org.codehaus.jackson.map.ObjectMapper._readMapAndClose(ObjectMapper.java:2728)
at org.codehaus.jackson.map.ObjectMapper.readValue(ObjectMapper.java:1870)

最佳答案

在人们面临同样的问题时发布答案。您可以使用此实用程序来解决您的问题

public static <T> T deserializeJson(final String json, final Class<T> clazz) throws JsonParseException, JsonMappingException, IOException {
ObjectMapper om = new ObjectMapper();
om.setVisibility(JsonMethod.FIELD, Visibility.ANY);
om.configure(DeserializationConfig.Feature.FAIL_ON_UNKNOWN_PROPERTIES, false);
return om.readValue(json, clazz);
}

public static <T> String serializeToJson(final T object) throws JsonGenerationException, JsonMappingException, IOException {
ObjectMapper om = new ObjectMapper();
om.setVisibility(JsonMethod.FIELD, Visibility.ANY);
return om.writeValueAsString(object);
}

关于java - 找不到非具体 Map 类型的反序列化器 [map type;类 javax.ws.rs.core.MultivaluedMap,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35468222/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com