Spring @ResponseBody返回List

时间:2016-03-06 19:42:18

标签: spring spring-mvc jackson

首先我试着发回一个列表响应(我使用AJAX,所以在完成的部分我期待列表响应):

@RequestMapping(value = "/a", method = RequestMethod.POST)
public @ResponseBody List<Xy> a( @RequestBody OtherClass oc, Model model) {
    ....codes
    List<Xy> objList = xyRepository.findAll();
    return objList;
}

然后这个:

@RequestMapping(value = "/a", method = RequestMethod.POST)
public @ResponseBody ListWrapper a( @RequestBody OtherClass oc, Model model) {
    ...codes
    List<Xy> objList = xyRepository.findAll();
    ListWrapper lw = new ListWrapper();
    lw.setObjList(objList);
    return lw;
}

然而,对于他们两个我得到了相同的错误,这个错误重复多次,但它不会永远循环,所以我可以在网站中导航。

:2.6.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serializeContents(CollectionSerializer.java:149) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:111) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.CollectionSerializer.serialize(CollectionSerializer.java:24) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.BeanSerializer.serialize(BeanSerializer.java:157) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.BeanPropertyWriter.serializeAsField(BeanPropertyWriter.java:693) ~[jackson-databind-2.6.5.jar:2.6.5]
    at com.fasterxml.jackson.databind.ser.std.BeanSerializerBase.serializeFields(BeanSerializerBase.java:675) ~[jackson-databind-2.6.5.jar:2.6.5]

1 个答案:

答案 0 :(得分:2)

看起来你在从存储库(xy)返回的类中有一个循环引用。

在BeanSerializer.157或堆栈跟踪的任何其他位置设置断点。然后你有机会看到serisalizer尝试序列化的属性。从那以后你应该能够找到有问题的财产。

找到有问题的属性后,可以将其从JSON序列化中排除。你可以用

做到这一点
@JsonIgnoreProperties({"foobar"})