我在通过jackson解析器、ajax和jQuery将对象数组传递给controller时遇到了问题。
我的控制器如下所示:
@RequestMapping(value="/saveTimeBooking")
public @ResponseBody ReturnTO saveTimeBooking(@RequestBody TimesheetTO timesheetTO[]) {
// ...
}
public class TimesheetTO implements Serializable {
private static final long serialVersionUID = 1L;
private int activityMasterId;
private String name;
private BigDecimal mondayEffort=new BigDecimal(0);
private BigDecimal tuesdayEffort=new BigDecimal(0);
private BigDecimal wednesdayEffort=new BigDecimal(0);
private BigDecimal thursdayEffort=new BigDecimal(0);
private BigDecimal fridayEffort=new BigDecimal(0);
private BigDecimal saturdayEffort=new BigDecimal(0);
private BigDecimal sundayEffort=new BigDecimal(0);
public int getActivityMasterId() {
return activityMasterId;
}
public String getName() {
return name;
}
public BigDecimal getMondayEffort() {
return mondayEffort;
}
public BigDecimal getTuesdayEffort() {
return tuesdayEffort;
}
public BigDecimal getWednesdayEffort() {
return wednesdayEffort;
}
public BigDecimal getThursdayEffort() {
return thursdayEffort;
}
public BigDecimal getFridayEffort() {
return fridayEffort;
}
public BigDecimal getSaturdayEffort() {
return saturdayEffort;
}
public BigDecimal getSundayEffort() {
return sundayEffort;
}
public void setActivityMasterId(int activityMasterId) {
this.activityMasterId = activityMasterId;
}
public void setName(String name) {
this.name = name;
}
public void setMondayEffort(BigDecimal mondayEffort) {
this.mondayEffort = mondayEffort;
}
public void setTuesdayEffort(BigDecimal tuesdayEffort) {
this.tuesdayEffort = tuesdayEffort;
}
public void setWednesdayEffort(BigDecimal wednesdayEffort) {
this.wednesdayEffort = wednesdayEffort;
}
public void setThursdayEffort(BigDecimal thursdayEffort) {
this.thursdayEffort = thursdayEffort;
}
public void setFridayEffort(BigDecimal fridayEffort) {
this.fridayEffort = fridayEffort;
}
public void setSaturdayEffort(BigDecimal saturdayEffort) {
this.saturdayEffort = saturdayEffort;
}
public void setSundayEffort(BigDecimal sundayEffort) {
this.sundayEffort = sundayEffort;
}
public TimesheetTO(){}
public TimesheetTO( String activityMasterId,
String name,
String mondayEffort,
String tuesdayEffort,
String wednesdayEffort,
String thursdayEffort,
String fridayEffort,
String saturdayEffort,
String sundayEffort)
{
this.activityMasterId=Integer.parseInt(activityMasterId);
this.name=name;
this.mondayEffort=BigDecimal.valueOf(Double.parseDouble(mondayEffort));
this.tuesdayEffort=BigDecimal.valueOf(Double.parseDouble(tuesdayEffort));
this.wednesdayEffort=BigDecimal.valueOf(Double.parseDouble(wednesdayEffort));
this.thursdayEffort=BigDecimal.valueOf(Double.parseDouble(thursdayEffort));
this.fridayEffort=BigDecimal.valueOf(Double.parseDouble(fridayEffort));
this.saturdayEffort=BigDecimal.valueOf(Double.parseDouble(saturdayEffort));
this.sundayEffort=BigDecimal.valueOf(Double.parseDouble(sundayEffort));
}
}
var datacounter=0;
/*var totalcount=0;*/
var timesheetTO = new Array();
$("#timeSheetTableId input[id^=activityMasterId]").each(function() {
$('#activityMasterId_'+datacounter).removeAttr('disabled');
$('#name_'+datacounter).removeAttr('disabled');
timesheetTO.push({
"activityMasterId": $('#activityMasterId_'+datacounter).val(),
"name": $('#name_'+datacounter).val(),
"mondayEffort": new Integer($('#mondayEffort_'+datacounter).val()),
"tuesdayEffort": $('#tuesdayEffort_'+datacounter).val(),
"wednesdayEffort": $('#wednesdayEffort_'+datacounter).val(),
"thursdayEffort": $('#thursdayEffort_'+datacounter).val(),
"fridayEffort": $('#fridayEffort_'+datacounter).val(),
"saturdayEffort": $('#saturdayEffort_'+datacounter).val(),
"sundayEffort": $('#sundayEffort_'+datacounter).val()
});
datacounter=datacounter+1;
});
var url = contextRoot + "timesheet/saveTimeBooking.htm";
$.ajax({
type : 'POST',
url : url,
async : false,
timeout : 5000,
contentType: 'application/json',
dataType: 'json',
data : {
timesheetTO : JSON.stringify(timesheetTO)
},
success : function(data, textStatus) {
alert('successful');
},
error : function(xhr, textStatus, errorThrown) {
alert('request failed in saving timesheet:' + errorThrown + " " + textStatus + " "
+ xhr.toString());
}
});
以下是json.stringify
的结果:
[{"activityMasterId":"1","name":"Financial Implications","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thursdayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"},{"activityMasterId":"2","name":"Cost estimation","mondayEffort":"0","tuesdayEffort":"0","wednesdayEffort":"0","thursdayEffort":"0","fridayEffort":"0","saturdayEffort":"0","sundayEffort":"0"}]
问题是您传递的字符串值缺少引号,因此编译器需要一个布尔/空对象。
您应该通过调试在运行时验证传递的值,必要时在文本编辑器中复制该值以彻底分析它。
我有一个模型 但我有个错误 有什么想法可以修复这个错误吗?
我创建了非常简单的流程,我已经附上了下面的图片。 流程描述: 用户发送费用报告,最后它得到审查,应该批准,而我是点击批准按钮。但我得到了如下错误: 变量上的当前json 请问json中的错误是什么,我在名为“onaccept1”的变量上犯了什么错误。
和ContactForm 我不知道到底发生了什么,因为如果我尝试向控制器发送一个带有POSTMAN的JSON,这个主体与相同,一切都很顺利,所以Jackson在幕后做了一些奇怪的事情...