我在使用org.codehaus.jackson.map.ObjectMapper进行反序列化时遇到以下问题,该问题不适用于以下类。我不确定我使用的多值地图出了什么问题。
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)
答案 0 :(得分:0)
在人们面临同样的问题时发布答案。您可以使用此工具来解决您的问题
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);
}