Java Spring Boot-只有对象的一些变量是绑定的
我正在尝试向我的spring boot应用程序发出AJAX post请求。Javascript似乎运行良好,因为所有值都在POST请求中填充——然而,当Commonage对象访问Java类时,我们只能看到11个字段中有3个带有值。该程序的输出为:Java Spring Boot-只有对象的一些变量是绑定的,java,ajax,spring-boot,binding,Java,Ajax,Spring Boot,Binding,我正在尝试向我的spring boot应用程序发出AJAX post请求。Javascript似乎运行良好,因为所有值都在POST请求中填充——然而,当Commonage对象访问Java类时,我们只能看到11个字段中有3个带有值。该程序的输出为: null 0 0 0 0 无效的 无效的 无效的 城镇 未设定 这是评论 JAVASCRIPT var commonage_id = document.getElementById("commonage_id").value; var townland
null
0
0
0
0
无效的
无效的
无效的
城镇
未设定
这是评论
JAVASCRIPT
var commonage_id = document.getElementById("commonage_id").value;
var townland = document.getElementById("edit_townland").value;
var gross_area = parseFloat(document.getElementById("edit_gross_area").value);
var max_elig = parseFloat(document.getElementById("edit_max_elig").value);
var min_stock = parseFloat(document.getElementById("edit_min_stock").value);
var max_stock = parseFloat(document.getElementById("edit_max_stock").value);
var start_date = document.getElementById("edit_start_date").value;
var end_date = document.getElementById("edit_end_date").value;
var owner_share = document.getElementById("edit_owner_share").value;
var type = document.getElementById("typeSelect").value;
var comment = document.getElementById("comment").value;
var commonage = {
commonage_id,
gross_area,
max_elig,
min_stock,
max_stock,
start_date,
end_date,
owner_share,
type,
townland,
comment
}
$.ajax({
url:"UpdateCommonage",
type:"POST",
data: JSON.stringify(commonage),
contentType:"application/json; charset=utf-8",
dataType:"json",
success: function(){
alert("success");
}
})`
公共类
import java.util.Date;
public class Commonage {
private String commonageIdentifier;
private double grossArea;
private double maxEligibleArea;
private int minStock;
private int maxStock;
private Date startDate;
private Date endDate;
private String ownerShare;
private String type;
private String townland;
private String comment;
public Commonage() {
}
public Commonage(String commonageIdentifier, double grossArea, double maxEligibleArea, int minStock,
int maxStock, Date startDate, Date endDate, String ownerShare, String type, String townland, String comment) {
this.commonageIdentifier = commonageIdentifier;
this.grossArea = grossArea;
this.maxEligibleArea = maxEligibleArea;
this.minStock = minStock;
this.maxStock = maxStock;
this.startDate = startDate;
this.endDate = endDate;
this.ownerShare = ownerShare;
this.type = type;
this.townland = townland;
this.comment = comment;
}
public String getCommonageIdentifier() {
return commonageIdentifier;
}
public void setCommonageIdentifier(String commonageIdentifier) {
this.commonageIdentifier = commonageIdentifier;
}
public double getGrossArea() {
return grossArea;
}
public void setGrossArea(double grossArea) {
this.grossArea = grossArea;
}
public double getMaxEligibleArea() {
return maxEligibleArea;
}
public void setMaxEligibleArea(double maxEligibleArea) {
this.maxEligibleArea = maxEligibleArea;
}
public int getMinStock() {
return minStock;
}
public void setMinStock(int minStock) {
this.minStock = minStock;
}
public int getMaxStock() {
return maxStock;
}
public void setMaxStock(int maxStock) {
this.maxStock = maxStock;
}
public Date getStartDate() {
return startDate;
}
public void setStartDate(Date startDate) {
this.startDate = startDate;
}
public Date getEndDate() {
return endDate;
}
public void setEndDate(Date endDate) {
this.endDate = endDate;
}
public String getOwnerShare() {
return ownerShare;
}
public void setOwnerShare(String ownerShare) {
this.ownerShare = ownerShare;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
}
public String getTownland() {
return townland;
}
public void setTownland(String townland) {
this.townland = townland;
}
public String getComment() {
return comment;
}
public void setComment(String comment) {
this.comment = comment;
}
}
控制器方法
@RequestMapping("UpdateCommonage")
@ResponseBody
public boolean updateCommonage(@RequestBody Commonage commonage) {
System.out.println(commonage.getCommonageIdentifier());
System.out.println(commonage.getGrossArea());
System.out.println(commonage.getMaxEligibleArea());
System.out.println(commonage.getMaxStock());
System.out.println(commonage.getMinStock());
System.out.println(commonage.getStartDate());
System.out.println(commonage.getEndDate());
System.out.println(commonage.getOwnerShare());
System.out.println(commonage.getTownland());
System.out.println(commonage.getType());
System.out.println(commonage.getComment());
return false;
}
默认情况下,Jackson(Spring Boot中使用的JSON(反)序列化程序)将在JSON中查找与Java类上的属性名完全匹配的属性名
有两种选择。推荐的最佳实践是不要在JavaScript中使用snake-case变量名。JavaScript变量名通常以驼峰大小写。因此,您可以将请求主体更改为:
var commonage = {
commonageIdentifier,
grossArea,
maxEligibleArea,
minStock,
maxStock,
startDate,
endDate,
ownerShare,
type,
townland,
comment
}
请注意,似乎唯一正确映射的属性是一个单词
另一个不太理想的选择是使用Java类上的@JsonProperty
注释告诉Jackson要在JSON上查找的属性名,如下所示:
public class Commonage {
@JsonProperty("commonage_id")
private String commonageIdentifier;
@JsonProperty("gross_area")
private double grossArea;
@JsonProperty("max_elig")
private double maxEligibleArea;
@JsonProperty("min_stock")
private int minStock;
@JsonProperty("max_stock")
private int maxStock;
@JsonProperty("start_date")
private Date startDate;
@JsonProperty("end_date")
private Date endDate;
@JsonProperty("owner_share")
private String ownerShare;
@JsonProperty("type")
private String type;
@JsonProperty("townland")
private String townland;
@JsonProperty("comment")
private String comment;
}
默认情况下,Jackson(Spring Boot中使用的JSON(反)序列化程序)将在JSON中查找与Java类上的属性名完全匹配的属性名
有两种选择。推荐的最佳实践是不要在JavaScript中使用snake-case变量名。JavaScript变量名通常以驼峰大小写。因此,您可以将请求主体更改为:
var commonage = {
commonageIdentifier,
grossArea,
maxEligibleArea,
minStock,
maxStock,
startDate,
endDate,
ownerShare,
type,
townland,
comment
}
请注意,似乎唯一正确映射的属性是一个单词
另一个不太理想的选择是使用Java类上的@JsonProperty
注释告诉Jackson要在JSON上查找的属性名,如下所示:
public class Commonage {
@JsonProperty("commonage_id")
private String commonageIdentifier;
@JsonProperty("gross_area")
private double grossArea;
@JsonProperty("max_elig")
private double maxEligibleArea;
@JsonProperty("min_stock")
private int minStock;
@JsonProperty("max_stock")
private int maxStock;
@JsonProperty("start_date")
private Date startDate;
@JsonProperty("end_date")
private Date endDate;
@JsonProperty("owner_share")
private String ownerShare;
@JsonProperty("type")
private String type;
@JsonProperty("townland")
private String townland;
@JsonProperty("comment")
private String comment;
}
您是否检查了ajax请求中实际发送的内容?是否检查了ajax请求中实际发送的内容?