When looking at my Spring controller and client-side Javascript code, I noticed that the Javascript object is having trouble reaching the Spring controller in Object form. Here is a snippet of my Controller code:
@RequestMapping(value = "/addRating", method = RequestMethod.POST, headers = "Accept=application/json")
public EmployeeRating addRating(@ModelAttribute("employeeRating") EmployeeRating employeeRating) {
if(employeeRating.getId()==0)
{
employeeRatingService.addRating(employeeRating);
}
else
{
employeeRatingService.updateRating(employeeRating);
}
return employeeRating;
}
And here is a glimpse of my Javascript code:
$.ajax({
url: 'https://myrestURL/addRating',
type: 'POST',
dataType: 'json',
data: {
'id':5,
'name': 'Name',
'rating': '1'
},
contentType: 'application/json; charset=utf-8',
success: function (result) {
// CallBack(result);
window.alert("Result: " + result);
},
error: function (error) {
window.alert("Error: " + error);
}
});
The Java EmployeeRating object entails id, name, and rating fields which should align perfectly with the setup.
I decided to update the model class structure:
import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.GeneratedValue;
import javax.persistence.GenerationType;
import javax.persistence.Id;
import javax.persistence.Table;
/*
* This represents our model class corresponding to the Country table in the database
*/
@Entity
@Table(name="EMPLOYEERATING")
public class EmployeeRating {
@Id
@Column(name="id")
@GeneratedValue(strategy=GenerationType.IDENTITY)
int id;
@Column(name="name")
String name;
@Column(name="rating")
long rating;
public EmployeeRating() {
super();
}
public EmployeeRating(int i, String name,long rating) {
super();
this.id = i;
this.name = name;
this.rating=rating;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public long getRating() {
return rating;
}
public void setRating(long rating) {
this.rating = rating;
}
}