简体   繁体   English

Spring RequestBody Mapping将所有属性映射为空值

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

i have the following RESTfull method : 我有以下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);
}

I'am consuming this method inside a web application, i checked using the network analysis tool (in the web developper tool of chrome) that the object sended is valid (all attribute except the id were set with a valid value), but then the object passed to the repository contains only null attributes. 我在网络应用程序中使用了这种方法,我使用网络分析工具(在chrome的网络开发工具中)检查了发送的对象是否有效(除id以外的所有属性均设置了有效值),但是传递到存储库的对象仅包含空属性。

here is an example body : 这是一个示例主体:

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

the class BudgetLine is defined as follows: 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() {
    }
}

Try with first letter in lowercase for parameters 尝试使用小写的首字母作为参数

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

Spring relies heavily on standard Java naming conventions, so I suggest you also follow them. Spring在很大程度上依赖于标准Java命名约定,因此我建议您也遵循它们。 In your example, you should name your class fields with lowercased first letter. 在您的示例中,您应该使用小写的首字母来命名类字段。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM