使用AJAX从Spring中的POST请求返回列表

时间:2017-05-12 21:34:19

标签: java ajax spring google-app-engine jackson

我目前在页面上有一个POST请求来检索课程列表。

$.ajax({
    headers: { 
      'Accept': 'application/json',
      'Content-Type': 'application/json' 
    },
    'type': 'POST',
    'url': '/search/request',
    'data': JSON.stringify(data),
    'dataType': 'json'
});

控制器中的相应方法返回课程列表:

@Controller
@EnableWebMvc
public class SearchController {
    // Some other methods

    @RequestMapping(value = "/search/request",  method = RequestMethod.POST)
    public @ResponseBody
    List<Lesson> search_lessons(@CookieValue("token") String token, @RequestBody SearchQuery query) {
        try {
            // Grab the user.

            // TODO: Implement search logic.
            List<Lesson> searchedLessons = lessonService.get_main_lessons_by_user(user);
            System.out.println(searchedLessons.size());
            return searchedLessons;
        }
        catch (Exception e) {
            System.out.println(e.getMessage());
            return null;
        }
    }
}

当我发出请求时,它会点击System.out.println(searchedLessons.size());,所以我知道列表正在填充。但是,进行AJAX调用会导致500错误,并且不会抛出任何异常。如何让Spring返回此列表?

编辑:这是Lesson

的结构
package com.dolphinblue.models;

import com.googlecode.objectify.Key;
import com.googlecode.objectify.annotation.Entity;
import com.googlecode.objectify.annotation.Id;
import com.googlecode.objectify.annotation.Index;

import java.util.*;
/**
 * Created by FreddyEstevez on 3/21/17.
 * Represent model for lessons
 */
@Entity
public class Lesson implements Comparable<Lesson>{

    @Id private Long lesson_id;
    private Long index;
    private String title;
    @Index private String user_id; //user who is working on the lesson
    @Index private String creator_id; //user who created the lesson
    private List<Key<Task>> tasks; //holds lists of tasks ids for this lesson
    private double percent_complete; // Hold the percent of task the user has completed
    @Index private boolean shared;
    @Index private  boolean site_owned;
    private Key<Lesson> original_lesson;
    private String description;
    private int rating;
    //Date when lesson was last edited or changed.
    private Date last_edited;
    //Date when this lesson was last access by user working on it
    private Date last_accessed;

`

1 个答案:

答案 0 :(得分:0)

因此,使用Objectify Keys删除任何字段都允许对象序列化。