简体   繁体   English

Spring Ajax列表返回但对象为空

[英]Spring Ajax List returned but objects empty

I am building something like a locker management system and the lockers have an update history. 我正在建立类似储物柜的管理系统,并且储物柜有更新历史。 I want use Ajax to return a list of of this history up to X entries in the list. 我想使用Ajax返回此历史记录的列表,最多可以返回列表中的X个条目。 Whenever I return List<LockerHistoryEntity> or Iterable<LockerHistoryEntity> , the amount of objects is returned, but all the objects are empty. 每当我返回List<LockerHistoryEntity>Iterable<LockerHistoryEntity> ,都会返回对象数量,但是所有对象都是空的。

Example of the problem 问题的例子

Ajax call (called upon with getHistory(100) in this case): Ajax调用(在这种情况下,使用getHistory(100)进行了调用):

function getHistory(limit) {
    var data = {};
    data["limit"] = limit;

    $.ajax({
        type: "POST",
        contentType: "application/json",
        url: "/gethistory",
        data: JSON.stringify(data),
        dataType: 'json',
        timeout: 100000,
        success : function(data) {
            fillTable(data);
            console.log(data);
            console.log(data.result);
        },
        error : function(e) {
            //error
        }
    });
}

@RestController @RestController

@JsonView(Views.Public.class)
    @RequestMapping(value = "/gethistory", method = RequestMethod.POST, produces="application/json")
    @ResponseBody
    public Iterable<LockerHistoryEntity> getHistory(@RequestBody HistoryLimit limit) {
        Iterable<LockerHistoryEntity> lockerHistory;

        if (limit.getLimit() >= 0) {
            lockerHistory = history.findAllLimit(limit.getLimit());
        } else {
            lockerHistory = history.findAll();
        }

        return lockerHistory;
    }

When using a for loop to print out lockerHistory the data is all displayed correctly. 使用for循环打印出lockerHistory ,所有数据均正确显示。

Sadly, I couldn't find any topic about this exact problem, hence this post. 可悲的是,我找不到关于这个确切问题的任何话题,因此这篇文章。

You need to convert the object into JSON form before sending the response to AJAX. 您需要先将对象转换为JSON格式,然后再将响应发送到AJAX。 See this link: http://howtodoinjava.com/jackson/jackson-examples-convert-java-object-to-from-json/ 看到此链接: http : //howtodoinjava.com/jackson/jackson-examples-convert-java-object-to-from-json/

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM