我使用Spring Boot 2
控制器部分
@PostMapping("template/new/samplings")
@ResponseBody
public SamplingsDto save(@ModelAttribute SamplingsDto samplings) {
return samplingsService.save(samplings);
}
我尝试保存表单
$("#samplingsForm").submit(function (e){
e.preventDefault();
var receptionDate = $("#samplingsReceptionDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');
var buildDate = $("#samplingsBuildDatePicker").data('daterangepicker').startDate.format('YYYY-MM-DD');
var form = transForm.serialize('#samplingsForm');
form.receptionDate=receptionDate;
form.buildDate=buildDate;
form = JSON.stringify(form);
$.ajax({
type:"post",
url: "/template/new/samplings",
data: form,
contentType: "application/json",
dataType : "json",
success: function(data){
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
}
});
});
使用Chrome请求有效负载是
“ {” “ samplingsId”:“”, “ buildDate”:“ 2018-06-20”, “ receptionDate”:“ 2018-06-20”, “ productTypesId”:“ 1”, “ productsId”:“ 15”, “}”
在服务器型号属性字段上为空
编辑
public class SamplingsDto {
private Integer samplingsId;
private Integer productTypesId;
private Integer productsId;
private LocalDate receptionDate;
private LocalDate buildDate;
//get set
}
答案 0 :(得分:1)
首先,为SamplingsDto的属性添加设置器和获取器。 (我还将Lombok用于设置者/获取者)
public class SamplingsDto {
private Integer samplingsId;
private Integer productTypesId;
private Integer productsId;
private LocalDate receptionDate;
private LocalDate buildDate;
public Integer getSamplingsId() {
return samplingsId;
}
public void setSamplingsId(Integer samplingsId) {
this.samplingsId = samplingsId;
}
public Integer getProductTypesId() {
return productTypesId;
}
public void setProductTypesId(Integer productTypesId) {
this.productTypesId = productTypesId;
}
public Integer getProductsId() {
return productsId;
}
public void setProductsId(Integer productsId) {
this.productsId = productsId;
}
public LocalDate getReceptionDate() {
return receptionDate;
}
public void setReceptionDate(LocalDate receptionDate) {
this.receptionDate = receptionDate;
}
public LocalDate getBuildDate() {
return buildDate;
}
public void setBuildDate(LocalDate buildDate) {
this.buildDate = buildDate;
}
}
然后您可以按以下方式使用它:
@PostMapping("template/new/samplings")
public SamplingsDto save(@RequestBody SamplingsDto samplings) {
return samplingsService.save(samplings);
}
答案 1 :(得分:0)
您需要创建rest webservice才能接收此请求。
添加@RestController
批注并将标头设置为application/json
。
希望这可以帮助您在此处解决问题。