У меня есть класс объектов GroupStudent, Spring Controller и JSP с функцией ajax. В контроллере я пытаюсь передать объект GroupStudent объекта на страницу JSP с помощью @ResponseBody. Но я всегда получаю ошибку от браузера: Error [object Object]. Я узнал, что мне нужно добавить в папку lib в проектах jackson-core-asl и jackson-mapper-asl jars. Я добавил их (версия 1.9.7) и поместил в spring-servlet.xml, чтобы он автоматически мог преобразовать объект GroupStudent в формат json и передать его обратно в функцию ajax. Но это не помогло, и у меня всегда есть одинаковое диалоговое окно ошибок в браузере. Если кто-то знает, как я могу передать объект объекта в ajax с помощью @ResponseBody, я буду очень благодарен за помощь.Ошибка Spring MVC @ResponseBody
спасибо.
класс 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 и используйте его, чтобы узнать, что делает ваш запрос, и полученный вами ответ, а затем проверьте свой сервер на наличие ошибок. –