我用弹簧引导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) {
}
});
});使用铬请求的有效负载是
{“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
}发布于 2018-06-20 20:21:25
首先,为SamplingsDto的属性添加setter和getter。(我也会将Lombok用于setter/getter)
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);
}发布于 2018-06-21 11:01:45
您需要创建rest rest服务来接收此请求。添加@RestController注释并将标头设置为application/json。我希望这能帮助你解决你的问题。
https://stackoverflow.com/questions/50956050
复制相似问题