这个简单的代码:
public static void Test() throws JsonProcessingException {
Map<Object, Object> request = new HashMap<>();
request.put("id", "test_0001");
request.put("version", 1);
Map<Object, Object> fields = new HashMap<>();
fields.put("uri", "blah/blah");
fields.put("owner", "me");
request.put("fields", request);
ObjectMapper om = new ObjectMapper();
System.out.println(om.writeValueAsString(request));
}
导致此异常:
Exception in thread "main" java.lang.StackOverflowError
at java.lang.Enum.ordinal(Enum.java:103)
at com.fasterxml.jackson.databind.MapperFeature.getMask(MapperFeature.java:259)
at com.fasterxml.jackson.databind.cfg.MapperConfig.isEnabled(MapperConfig.java:110)
at com.fasterxml.jackson.databind.SerializationConfig.getAnnotationIntrospector(SerializationConfig.java:404)
at com.fasterxml.jackson.databind.SerializerProvider.getAnnotationIntrospector(SerializerProvider.java:307)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.createContextual(MapSerializer.java:235)
at com.fasterxml.jackson.databind.SerializerProvider._handleContextual(SerializerProvider.java:968)
at com.fasterxml.jackson.databind.SerializerProvider.findValueSerializer(SerializerProvider.java:447)
at com.fasterxml.jackson.databind.ser.impl.PropertySerializerMap.findAndAddSerializer(PropertySerializerMap.java:38)
at com.fasterxml.jackson.databind.ser.std.MapSerializer._findAndAddDynamic(MapSerializer.java:516)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serializeFields(MapSerializer.java:386)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:312)
at com.fasterxml.jackson.databind.ser.std.MapSerializer.serialize(MapSerializer.java:26)
etc...
对于我的生活,我无法弄清楚为什么。我通过搜索找到的所有内容都是由于递归引用而出现问题的人,但在这种情况下并非如此。
答案 0 :(得分:3)
您已将Map
置于其中
request.put("fields", request);
这会产生无限循环。你不应该把Map
放进去。
你可能打算写
request.put("fields", fields);
答案 1 :(得分:1)
此行在数据中创建一个循环:
request.put("fields", request);
这正是递归引用的样子。