I have entity class GroupStudent, Spring Controller and JSP page with ajax function. In Controller I try to pass entity GroupStudent object to JSP page using @ResponseBody. But I always get error from browser: Error[object Object]. I found out that i need to add to lib folder in project jackson-core-asl and jackson-mapper-asl jars. I added them (version 1.9.7) and put to spring-servlet.xml so that it automatically could convert GroupStudent object to json format and pass it back to ajax function. But it did not help and I always have the same error dialog in browser. If someone knows how can i pass entity object to ajax using @ResponseBody I will be very gratefull for the help.
Thank you.
GroupStudent class
@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;
}
Controller
@RequestMapping(value = "/addData.html", method = RequestMethod.POST)
public @ResponseBody GroupStudent addNewGroup(@RequestBody GroupStudent group) {
return group;
}
}
Ajax function
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);
}
});
}