gpt4 book ai didi

java - Spring-Boot - 只有对象的某些变量是绑定(bind)的

转载 作者:行者123 更新时间:2023-12-02 02:14:51 24 4
gpt4 key购买 nike

我正在尝试向我的 Spring Boot 应用程序发出 AJAX post 请求。 Javascript 似乎工作正常,因为所有值都填充在 POST 请求中 - 然而,当 Commonage 对象命中 Java 类时,我们只能看到 11 个字段中的 3 个已带有值。该程序的输出是:


0.0
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;
}

}

Controller 方法

@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 中使用蛇形变量名称。通常,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;
}

关于java - Spring-Boot - 只有对象的某些变量是绑定(bind)的,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49453967/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com