【问题标题】:Spring MVC @ResponseBody errorSpring MVC @ResponseBody 错误
【发布时间】:2013-11-11 19:58:28
【问题描述】:

我有实体类 GroupStudent、Spring Controller 和带有 ajax 功能的 JSP 页面。在控制器中,我尝试使用@ResponseBody 将实体 GroupStudent 对象传递给 JSP 页面。但我总是从浏览器收到错误:Error[object Object]。我发现我需要添加到项目 jackson-core-asl 和 jackson-mapper-asl jar 中的 lib 文件夹中。我添加了它们(版本 1.9.7)并放入 spring-servlet.xml 以便它可以自动将 GroupStudent 对象转换为 json 格式并将其传递回 ajax 函数。但这没有帮助,我在浏览器中总是有相同的错误对话框。如果有人知道我如何使用 @ResponseBody 将实体对象传递给 ajax,我将非常感谢您的帮助。

谢谢。

GroupStudent 班级

@Entity
@Table(name = "GroupStudent")
@NamedQueries({ 
@NamedQuery(name = "GroupStudent.getAllGroups", // get all groups
            query = "select g from GroupStudent g"),
@NamedQuery(name = "GroupStudent.getGroupByName", // get group by name
            query = "select g from GroupStudent g where g.groupStudentNumber = :name")
})
public class GroupStudent implements Serializable {
public GroupStudent() {}

public GroupStudent(String groupStudentNumber) {
    this.groupStudentNumber = groupStudentNumber;
}

// create connectivity with table Student
private Set<Student> students = new HashSet<Student>();

@OneToMany(mappedBy = "groupStudent", cascade = CascadeType.ALL, orphanRemoval = true)
public Set<Student> getStudents() {
    return this.students;
}   

public void setStudents(Set<Student> students) {
    this.students = students;
}   

@Id
@GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "group_id_seq")
@SequenceGenerator(name = "group_id_seq", sequenceName = "GroupStudent_seq", allocationSize = 1)
@Column(name = "GroupStudentId")
public Long getGroupStudentId() {
    return this.groupStudentId;
}

public void setGroupStudentId(Long groupStudentId) {
    this.groupStudentId = groupStudentId;
}

@Column(name = "GroupStudentNumber")
public String getGroupStudentNumber() {
    return this.groupStudentNumber;
}

public void setGroupStudentNumber(String groupStudentNumber) {
    this.groupStudentNumber = groupStudentNumber;
}

// table GroupStudent fields
private Long groupStudentId;
private String groupStudentNumber;

}

控制器

@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody GroupStudent addNewGroup(@RequestBody GroupStudent group) {

    return group;
}

}

Ajax 函数

function addGroupAjax() {
            var groupStudentNumber = $('#groupStudentNumber').val();

            $.ajax({
                type: "POST",
                url: "/IRSystem/addData.html",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                mimeType: "application/json",
                data: "{\"groupStudentNumber\":" + "\"" + groupStudentNumber + "\"}",
                success: function(response) {

                },
                error: function(e) {
                    alert("Error" + e);
                }
            });
        } 

【问题讨论】:

  • 下载并安装 firebug 插件并使用它来查看您发出的请求和收到的响应是什么,然后检查您的服务器是否有任何错误日志。

标签: java ajax json spring-mvc


【解决方案1】:

您可以尝试将produces="application/json" 添加到您的@RequestMapping,使其看起来像@RequestMapping(value = "/addData.html", method = RequestMethod.POST, produces="application/json")

另外,我一开始没有看到它,您缺少 POST 方法上的 @ResponseBody 注释。在这篇博文http://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html 中,您可以看到它的作用。简短回答:它可以帮助您进行序列化/反序列化,并将您的序列化对象直接写入响应流,因此您不必手动执行。

另外,检查您的登录服务器以查看是否有任何错误,如果没有(如果您使用的是 Chrome),请打开 Settings -> Tools -> Developer tools -> Network ,然后再次触发您的呼叫。从列表中选择显示您的调用的元素,您可以在那里准确地看到服务器返回给您的内容。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2013-06-21
    • 2017-05-25
    • 2012-09-02
    • 2016-07-19
    • 2011-11-12
    • 2016-07-08
    • 2016-08-18
    相关资源
    最近更新 更多