Spring-Boot - 只有对象的某些变量是绑定的

时间:2018-03-23 16:09:27

标签: java ajax spring-boot binding

我正在尝试向我的spring启动应用程序发出一个AJAX post请求。 Javascript似乎工作正常,因为所有值都填充在POST请求中 - 但是当Commonage对象访问Java类时,我们只能看到11个字段中的3个已经通过值获得。该计划的输出是:

null 0.0 0.0 0 0 null null null TOWNLAND Not Set This is the comment

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

1 个答案:

答案 0 :(得分:2)

默认情况下,Jackson(Spring Boot中使用的JSON(de)序列化程序)将在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;
}