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);
}
我在 Web 应用程序中使用此方法,我使用网络分析工具(在 chrome 的 Web 开发人员工具中)检查发送的对象是否有效(除了 id 之外的所有属性都已设置具有有效值),但传递到存储库的对象仅包含空属性。
这里是一个例子:
{
"Name":"testLabel",
"Label":"testName",
"AnnualBudget":9000
}
class 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 命名约定,因此我建议您也遵循它们。在您的示例中,您应该使用小写首字母命名 class 字段。
我有以下 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);
}
我在 Web 应用程序中使用此方法,我使用网络分析工具(在 chrome 的 Web 开发人员工具中)检查发送的对象是否有效(除了 id 之外的所有属性都已设置具有有效值),但传递到存储库的对象仅包含空属性。
这里是一个例子:
{
"Name":"testLabel",
"Label":"testName",
"AnnualBudget":9000
}
class 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 命名约定,因此我建议您也遵循它们。在您的示例中,您应该使用小写首字母命名 class 字段。