【发布时间】:2018-08-07 10:56:43
【问题描述】:
POST 请求正文 json 值:
{
"id" : 452312313231,
"name" : "b2",
"floor": 5
}
建筑类代码:
@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;
}
控制器代码:
@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";
}
}
我使用 jpa 来保存数据。为了便于验证,我选择了 @ModelAttribute 而不是 @RequestBody。问题是建筑对象总是空的。 我在用 json 对象做任何事情吗?
【问题讨论】:
标签: json spring modelattribute