使用SpringMVC和Jackson2 </string,>获取JSON Map <string,object =“”>时出错

时间:2014-09-26 13:28:09

标签: json spring spring-mvc java-ee jackson

我正在尝试获取一个地图,其中'数字'包含操作数,'操作'包含操作列表。 我已经检查了java中的操作列表,它运行得很好。

我有4个动作,在json文件中,我正好在第一个。

java中的控制器

@RequestMapping(value = "/getactions/{idTask}", method = RequestMethod.GET, produces = "application/json")
@ResponseBody
public Map<String, Object> getActions(Principal principal,
        @PathVariable Long idTask) {
    logger.info("Task controller get actions...");

    List<TaskAction> actions = null;

    if (principal == null) {
        actions = new ArrayList<TaskAction>();
    } else {
        actions = taskActionService.getAllTaskActions(idTask);
    }

    System.out.println(">>>>>>>>Task Controller - Actions>>>>>>>>>>>>> "
            + actions.size());
    for (TaskAction ta : actions) {
        System.out.println(">>>>>>>>ta: " + ta.getActionname());
    }
    System.out.println(">>>>>>>>END getAllActions()>>>>>>>>>>>>> ");

    Map<String, Object> data = new HashMap<String, Object>();
    data.put("actions", actions);
    data.put("number", actions.size());

    return data;
}

SYSOUT

>>>>>>>>Task Controller - Actions>>>>>>>>>>>>> 4
>>>>>>>>ta: tacometer
>>>>>>>>ta: hola
>>>>>>>>ta: hi there
>>>>>>>>ta: other action
>>>>>>>>END getAllActions()>>>>>>>>>>>>> 

我没有收到任何错误,但json结果只获取动作列表中的第一个元素和其余动作的id。

json,我从http ... getactions / 8

获得
{  
   "number":4,
   "actions":[  
      { ... },
      3,
      4,
      5
   ]
}

......是第一个恢复良好的行动。 (我已经避免编写清晰的代码)

关于它可能是什么的任何想法?

提前致谢。


带有2个动作的json示例

{
    "number": 2,
    "actions": [{
        "idTaskAction": 4,
        "task": {
            "idTask": 8,
            "taskname": "abbbbbbbbbbbbbb",
            "description": "fffffffffffffffqqqqq",
            "date": 1389569940000,
            "deadline": -23918633280000,
            "category": {
                "idTaskCategory": 1,
                "sortOrder": 0,
                "categoryname": "cat1",
                "timestamp": 1402437394000
            },
            "priority": {
                "idTaskPriority": 1,
                "sortOrder": 0,
                "aka": "none",
                "priorityname": "low",
                "timestamp": 1402437527000
            },
            "state": {
                "idTaskState": 1,
                "statename": "pending"
            },
            "user": {
                "idUser": 1,
                "username": "joe"
            },
            "userResponsible": {
                "idUser": 1,
                "username": "joe"
            },
            "evaluation": "12345678saad",
            "pending": 0,
            "actions": [{
                "idTaskAction": 2,
                "task": 8,
                "date": 1402652358000,
                "actionname": "tacometer",
                "description": "asfdafa",
                "duration": 12,
                "user": {
                    "idUser": 1,
                    "username": "joe"
                },
                "timestamp": 1411493866000
            },
            {
                "idTaskAction": 3,
                "task": 8,
                "date": 1404207558000,
                "actionname": "hola",
                "description": "un dos tres",
                "duration": 20,
                "user": {
                    "idUser": 1,
                    "username": "joe"
                },
                "timestamp": 1405022827000
            },
            4,
            {
                "idTaskAction": 5,
                "task": 8,
                "date": 1412164741000,
                "actionname": "other action",
                "description": "ya me my",
                "duration": 22,
                "user": {
                    "idUser": 1,
                    "username": "joe"
                },
                "timestamp": 1411733131000
            }],
            "timestamp": 1411733131000
        },
        "date": 1412074440000,
        "actionname": "hi there",
        "description": "lkjñlkj ya",
        "duration": 25,
        "user": {
            "idUser": 1,
            "username": "joe"
        },
        "timestamp": 1411733090000
    },
    5]
}

1 个答案:

答案 0 :(得分:0)

问题是类Task,User ...有@JsonIdentityInfo和ObjectIdGenerator.PropertyGenerator.class选项,所以当Jackson尝试将对象序列化为json时,如果对象已经在文件中,那么它不是又补充道。这样就避免了使用无限递归序列化json文件。

要解决此问题,我们需要为 ObjectIdGenerator.None.class 更改ObjectIdGenerator.PropertyGenerator.class,以便即使对象已经是其他对象的一部分,也会对这些对象进行序列化。

更改

@JsonIdentityInfo(generator = ObjectIdGenerators.PropertyGenerator.class, property = "@id")

@JsonIdentityInfo(generator = ObjectIdGenerators.None.class, property = "@id")