从Spring控制器返回一个Object List到AJAX请求

时间:2016-09-01 13:05:45

标签: java jquery ajax spring spring-mvc

我想将一个Spring对象的List返回给一个AJAX请求。

AJAX请求:

function doAjaxPost(date) {
    $.ajax({
        type : "POST",
        contentType : "application/json",
        url : "<%="/water-consumer" + homepageURL + "/hourConsumption"%>",
        data : date,
        dataType : 'json',
        success : function(response) {
            if(response != null)
                showHourConsumption(response);
        }
    });
}

Spring控制器:

@RequestMapping(value = "/{username}/hourConsumption", method=RequestMethod.POST, produces="application/json")
public @ResponseBody List<HourConsumption> getHoursConsumptions(@RequestBody String day, HttpSession session, Model model) {
    // do something ....
    Household household = (Household)session.getAttribute("household");
    List<HourConsumption> consumptions = this.mrs.getHoursConsumption(household.getSmart_meter().getOid(), day);
    // do something 

    if(consumptions == null) //check if what get back from something is not null
        return null;
    else
        return consumptions; 
}

现在,AJAX请求正确,所以我收到来自控制器的请求并生成正确的消耗(列表)但是当它返回List时,浏览器会给我回复以下内容错误

  

无法加载资源:服务器响应状态为406   (不可接受)

有人可以告诉我,我该怎么做或正确的方法呢? 非常感谢你!

4 个答案:

答案 0 :(得分:0)

一些指示:

  1. http方法可以是GET iso POST。
  2. 删除会话和模型参数。如果您需要会话,则可以从自动装配HttpServletRequest
  3. 获取
  4. 确保你有杰克逊作为依赖

答案 1 :(得分:0)

确保类路径中存在以下2个jar。

如果缺少任何一个或两个,则会出现此错误。

jackson-core和jackson-mapper

答案 2 :(得分:0)

问题在于您返回的数据,浏览器没有很好地形成JSON。然后我会从你的Controller中删除属性“产生”,并保留Spring Jackson变换器来创建json实体。

将您的代码更改为:

@RequestMapping(value = "/{username}/hourConsumption", method=RequestMethod.POST)
public @ResponseBody List<HourConsumption> getHoursConsumptions(@RequestBody String day, HttpSession session, Model model) {
    // do something ....
    Household household = (Household)session.getAttribute("household");
    List<HourConsumption> consumptions = this.mrs.getHoursConsumption(household.getSmart_meter().getOid(), day);
    // do something 

    if(consumptions == null) //check if what get back from something is not null
        return null;
    else
        return consumptions; 
}

答案 3 :(得分:0)

尝试使用Google的gson(https://mvnrepository.com/artifact/com.google.code.gson/gson)将Json格式字符串发送到页面。

Spring Controller

@RequestMapping(value = "/{username}/hourConsumption", method=RequestMethod.POST)
public @ResponseBody String getHoursConsumptions(@RequestBody String day, HttpSession session, Model model) {
    String jsonFormatData = "";
    // do something ....
    Household household = (Household)session.getAttribute("household");
    List<HourConsumption> consumptions = this.mrs.getHoursConsumption(household.getSmart_meter().getOid(), day);
    // do something 


    // **Using Gson**
    // You can include this in your bean definition and autowire it
    // and use the singleton created by Spring.
    // For this example I am instantiating Gson myself
    Gson gson = new Gson(); 

    jsonFormatData = gson.toJson(consumptions);

    return jsonFormatData; 
}