简体   繁体   中英

Returning a list from a POST request in Spring using AJAX

I currently have a POST request on a page to retrieve a list of lessons.

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

The corresponding method in the controller returns a list of lessons:

@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;
        }
    }
}

When I make the request, it hits the System.out.println(searchedLessons.size()); , so I know the list is being populated. However, making the AJAX call results in a 500 error, and no exceptions are thrown. How do I get Spring to return this list?

EDIT: This is the structure of a 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;

`

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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