是否可以在Spring MVC中使用@ResponseBody返回JSONArray?

时间:2015-02-24 19:51:47

标签: ajax json spring-mvc

我尝试使用AJAX加载一个将从我的控制器返回数据的URL,我需要数据为JSONArray,但是当它进行调用时,我得到406不可接受的错误。我可以用@ResponseBody返回这个吗?我似乎无法在线找到解决方案。我已经将Jackson依赖项添加到了我的项目中。

我的AJAX电话:

$.ajax({
    url : '/TeamBravo/graphs/dimple/WEEK',
    success : function(data) {
        var dataForDimpleWeek = data;
    }
});

我的控制器:

@RequestMapping("/dimple/{timeScale}")
@ResponseBody
public JSONArray getDimpleData(@PathVariable("timeScale") String timeScale){

    JSONArray tweetsForDimple = new JSONArray();
    if(timeScale.equals("WEEK")){
        tweetsForDimple = getGraphWeekData(); //Returns JSONArray
    }else if(timeScale.equals("MONTH")){
        tweetsForDimple = getGraphMonthData();
    }
    return tweetsForDimple;
}

任何帮助都将非常感谢,谢谢。

1 个答案:

答案 0 :(得分:2)

我其实只是回答了这样的帖子,请在这里查看我的答案:Converting Java object to JSONObject and transmit it at GET method.