0

这个简单的代码:

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...

而对于我的生活,我无法弄清楚为什么。我通过搜索找到的只是人们因为递归引用而遇到问题,但在这种情况下并非如此。

4

2 回答 2

6

Map你已经把一个

request.put("fields", request);

这形成了一个无限循环。你不应该把它Map放进去。

你可能打算写

request.put("fields", fields);
于 2013-07-31T21:10:08.313 回答
1

此行在数据中创建一个循环:

request.put("fields", request);

这正是递归引用的样子。

于 2013-07-31T21:09:57.567 回答