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);
}
});
}
You could try to add produces="application/json"
to Your @RequestMapping
so that it looks like @RequestMapping(value = "/addData.html", method = RequestMethod.POST, produces="application/json")
.
Also, i didn't see it in the first place, you are missing the @ResponseBody
annotation on you POST method. In this blogpost http://www.javacodegeeks.com/2013/07/spring-mvc-requestbody-and-responsebody-demystified.html you can see what it does. Short answer: It helps you with serialization/deserialization and writes your serialized object directly to response stream, so you don't have to do it manually.
Also, check your log on server to see if there are any errors,if not (if you are using Chrome), open Settings -> Tools -> Developer tools -> Network , and fire your call again. Chose the element from the list that shows your call, and there you can see exactly what server returned you.