POST request body json value :
{
"id" : 452312313231,
"name" : "b2",
"floor": 5
}
code of Building class :
@Entity
@Table(name = "building")
public class Building implements Serializable{
private static final long serialVersionUID = -3009157732242241606L;
@Id
@Column(name = "building_ID")
@NotNull
@Digits(fraction = 0, integer = 15)
private long id;
@Column(name = "NAME")
@NotNull
private String name;
@Column(name = "floor")
@NotNull
@Digits(fraction = 0, integer = 2)
private int floor;
public Building() {}
public Building(long id, String name, int floor) {
this.id = id;
this.name = name;
this.floor = floor;
}
Code of controller :
@RequestMapping(value = "/building", method = RequestMethod.POST, headers = "Accept=application/json")
@ResponseBody
String post(Model model, @Valid @ModelAttribute("Building") Building building, BindingResult result) {
if (result.hasErrors()) {
System.out.println("Wrong data");
return "Null values or worng data please check properly Number = " + result.getErrorCount() + " \n Error : "
+ result.getAllErrors();
} else {
buildingRep.save(building);
return "Succesfull";
}
}
im using jpa to save data . and i chosed @ModelAttribute over @RequestBody for easy validation . Problem is building object is always empty. im i doing anything worng with json object ?