簡體   English   中英

Spring RequestBody Mapping將所有屬性映射為空值

[英]Spring RequestBody Mapping maps all attributes to null values

我有以下RESTfull方法:

    @RequestMapping(value = "/budgetLines",
        method = RequestMethod.POST,
        produces = MediaType.APPLICATION_JSON_VALUE)
@Timed
public void create(@RequestBody BudgetLine budgetLine) {
    System.out.println("Before Persisting in the repository " + budgetLine);
    budgetLineRepository.save(budgetLine);
}

我在網絡應用程序中使用了這種方法,我使用網絡分析工具(在chrome的網絡開發工具中)檢查了發送的對象是否有效(除id以外的所有屬性均設置了有效值),但是傳遞到存儲庫的對象僅包含空屬性。

這是一個示例主體:

{
    "Name":"testLabel",
    "Label":"testName",
    "AnnualBudget":9000
}

BudgetLine類的定義如下:

@Entity
@Table(name = "T_BUDGETLINE")
@Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
public class BudgetLine implements Serializable {

    @Id
    @GeneratedValue(strategy = GenerationType.AUTO)
    private Long id;

    @Column(name = "label")
    private String Label;

    @Column(name = "name")
    private String Name;

    @Column(name = "annual_budget", precision=10, scale=2)
    private BigDecimal AnnualBudget;

    @OneToMany(mappedBy = "budgetLine")
    @JsonIgnore
    @Cache(usage = CacheConcurrencyStrategy.NONSTRICT_READ_WRITE)
    private Set<Report> reportss = new HashSet<>();

    public Long getId() {
        return id;
    }

    public void setId(Long id) {
        this.id = id;
    }

    public String getLabel() {
        return Label;
    }

    public void setLabel(String Label) {
        this.Label = Label;
    }

    public String getName() {
        return Name;
    }

    public void setName(String Name) {
        this.Name = Name;
    }

    public BigDecimal getAnnualBudget() {
        return AnnualBudget;
    }

    public void setAnnualBudget(BigDecimal AnnualBudget) {
        this.AnnualBudget = AnnualBudget;
    }

    public Set<Report> getReportss() {
        return reportss;
    }

    public void setReportss(Set<Report> Reports) {
        this.reportss = Reports;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) {
            return true;
        }
        if (o == null || getClass() != o.getClass()) {
            return false;
        }

        BudgetLine budgetLine = (BudgetLine) o;

        if (id != null ? !id.equals(budgetLine.id) : budgetLine.id != null) return false;

        return true;
    }

    @Override
    public int hashCode() {
        return (int) (id ^ (id >>> 32));
    }

    @Override
    public String toString() {
        return "BudgetLine{" +
                "id=" + id +
                ", Label='" + Label + "'" +
                ", Name='" + Name + "'" +
                ", AnnualBudget='" + AnnualBudget + "'" +
                '}';
    }

    public BudgetLine() {
    }
}

嘗試使用小寫的首字母作為參數

{
    "name":"testLabel",
    "label":"testName",
    "annualBudget":9000
}

Spring在很大程度上依賴於標准Java命名約定,因此我建議您也遵循它們。 在您的示例中,您應該使用小寫的首字母來命名類字段。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM