我的模型如下,并添加使用spring mvc。
@Entity
@Table(name="audit_report")
public class AuditReport implements Serializable{
/**
*
*/
private static final long serialVersionUID = 1L;
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "audit_report_id")
private int auditReportId;
@ManyToOne(fetch=FetchType.LAZY,optional = false,cascade=CascadeType.ALL)
@JoinColumn(name="audit_factory_id")
private AuditFactory auditFactory;
@Column(name = "report_id")
private String reportId;
@Column(name = "editor")
private String editor;
@Column(name = "engineer_name")
@NotNull
private String engineerName;
@Column(name="service_date")
@DateTimeFormat(pattern="MM/dd/yyyy")
private Date serviceDate;
@Column(name="audit_date")
@DateTimeFormat(pattern="MM/dd/yyyy")
private Date auditDate;
@OneToMany(cascade = CascadeType.ALL ,fetch = FetchType.LAZY, mappedBy = "auditReport")
@Fetch(FetchMode.SUBSELECT)
private List<Printer> printers;
控制器如下所示,它将获取模型并保存。
@ResponseBody
@RequestMapping(value = "/saveAuditReport",method = RequestMethod.POST)
public Map<String,String> newAuditReport(HttpServletRequest request,@RequestBody AuditReport report){
ajax,如果添加serviceDate会出现400错误,删除serviceDate后就可以了
$.ajax({
type:"post",
url:"<%=path%>/audit/saveAuditReport.do",
contentType : 'application/json; charset=utf-8',
data:JSON.stringify({'serviceDate':pnDate,'engineerName':engineer,'reportId':reportId,"auditReportId":auditReportId,"printers":array,"auditFactory":{"auditFactoryId":factoryId}}),
dataType:'json',
success:function(data, textStatus){
if(data!=null){
if(data.error==null){
layer.msg(data.ok,1,9);
layer.close(pageii);
searchReportById(factoryId,obj);
}else{
layer.msg(data.error);
}
}
},
error : function(XMLHttpRequest, textStatus, errorThrown) {
}
});
ajax,如果添加serviceDate会出现400错误,删除serviceDate后就可以了
答案 0 :(得分:0)
首先,你的模型应该有setter和getter。然后,一个可能的问题是模型中的auditReportId
字段。您使用了@Id
和@GeneratedValue
注释。这意味着用户不应提供auditReportId
,而hibernate会自动生成它。但您手动分配了它。因此,您应该从json数据中删除"auditReportId":auditReportId
部分。试试这个,如果问题没有解决,请发布确切的异常堆栈跟踪以获得更好的帮助。
答案 1 :(得分:0)
您有date
解析错误...因此在模型类的日期字段上方添加@JsonSerialize(using = DateSerializer.class)
注释
@DateTimeFormat(pattern="MM/dd/yyyy")
@JsonSerialize(using = DateSerializer.class)
private Date serviceDate;