Java Spring MVC@ResponseBody错误
我有实体类GroupStudent、Spring控制器和带有ajax功能的JSP页面。在控制器中,我尝试使用@ResponseBy将实体GroupStudent对象传递给JSP页面。但我总是从浏览器中得到错误:error[object]。我发现我需要添加到项目jackson core asl和jackson mapper asl jars的lib文件夹中。我添加了它们(版本1.9.7)并将其放在spring-servlet.xml中,这样它就可以自动将GroupStudent对象转换为json格式,并将其传递回ajax函数。但它并没有帮助,我总是在浏览器中有相同的错误对话框。如果有人知道如何使用@ResponseBody将实体对象传递给ajax,我将非常感谢您的帮助 多谢各位 小组学生班Java Spring MVC@ResponseBody错误,java,ajax,json,spring-mvc,Java,Ajax,Json,Spring Mvc,我有实体类GroupStudent、Spring控制器和带有ajax功能的JSP页面。在控制器中,我尝试使用@ResponseBy将实体GroupStudent对象传递给JSP页面。但我总是从浏览器中得到错误:error[object]。我发现我需要添加到项目jackson core asl和jackson mapper asl jars的lib文件夹中。我添加了它们(版本1.9.7)并将其放在spring-servlet.xml中,这样它就可以自动将GroupStudent对象转换为json格
@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;
}
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);
}
});
}
您可以尝试将
products=“application/json”
添加到@RequestMapping
中,使其看起来像@RequestMapping(value=“/addData.html”,method=RequestMethod.POST,products=“application/json”)
另外,我一开始没有看到它,您缺少了POST方法上的@ResponseBody
注释。在这篇博文中,你可以看到它的作用。简短回答:它帮助您进行序列化/反序列化,并将序列化对象直接写入响应流,因此您不必手动执行
另外,检查您在服务器上的登录是否有任何错误,如果没有(如果您使用的是Chrome),请打开设置->工具->开发人员工具->网络,然后再次拨打电话。从显示您呼叫的列表中选择元素,在那里您可以准确地看到服务器返回给您的内容。下载并安装firebug插件,使用它查看您发出的请求和收到的响应,然后检查您的服务器是否有任何错误日志。
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);
}
});
}