7

我正在尝试获取要在 Spring 3 MVC 应用程序上呈现的对象列表,并希望通过 Ajax 执行此操作。

所以在我的春季课上,我有:

@RequestMapping(value = "/viewSearchEnquiriesAjax", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody List<Enquiry> getEnquiriesBySearchAjax(@RequestParam String name) {
    Search search =  new Search();
    search.setFirstName(name);
    return searchEnquiries(search);
}

但是当它运行时我得到一个 500(内部服务器错误)。当我在浏览器中调试时,这会表现出来'GET http://localhost:8080/SpringMVC/viewSearchEnquiriesAjax?name=peter 500 (Internal Server Error)'

我可以成功返回一个没有错误的对象。Spring Json 映射器(Jackson)可以正确转换吗?我错过了一些基本的东西吗?

我的javascript如下:

function doAjaxPost() {
// get the form values
var firstName = $('#firstName').val();
$.getJSON("/SpringMVC/viewSearchEnquiriesAjax", { name: firstName }, function(result) {
    alert("Success");
 });

}

我的查询对象是一个实体:

@Entity
@Table(name = "enquiries")
public class Enquiry implements java.io.Serializable{

    private static final long serialVersionUID = -5093725544297637792L;

    protected Long id;
    protected Date created = new Date();
    ...
    ...

    public Enquiry() {
    }

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    @Column(name = "id", unique = true, nullable = false)
    public Long getId() {
        return this.id;
    }

    public void setId(Long id) {
        this.id = id;
    }
   ...
   ...
4

1 回答 1

9

对于 Jackson,由于类型擦除,您必须创建一个强类型列表类:

public class EnquiryList extends ArrayList<Enquiry> {
}

然后从您的控制器返回此列表:

@RequestMapping(value = "/viewSearchEnquiriesAjax", method = RequestMethod.GET, headers = "Accept=application/json")
public @ResponseBody EnquiryList getEnquiriesBySearchAjax(@RequestParam String name) {
    EnquiryList list = new EnquiryList();
    ...
    return list;
}

另请查看有关类似问题的答案。

于 2012-10-07T20:19:27.753 回答