SpringBoot忽略了我的 @RequestBody 参数?

huangapple go评论133阅读模式
英文:

SpringBoot ignoring my @RequestBody param?

问题

我有以下的控制器:

@Validated
@RestController
public class ProductController {
  @Autowired
  private ProductService productService;

  @PutMapping("/products/{productId}/costs")
  public ResponseEntity<Product> updateProductCost(@RequestHeader String authorization, @PathVariable UUID productId, @RequestBody ProductCost productCost) {
    Product updatedProduct = productService.updateProductCost(productId, productCost);

    return ResponseEntity.ok(updatedProduct);
  }
}

ProductCost 模型如下:

@Data
@NoArgsConstructor
@Entity
@Table(name = "product_costs", schema = "mws")
public class ProductCost implements Serializable {
  private static final long serialVersionUID = 1789128204447938816L;

  @Column
  private Double unitCost;

  @Column
  private Double shippingCost;

  @Column
  private Double pickPack;

  @Column
  private Double weightHandling;

  @Column
  private Double handling;

  @Column
  private Double fbaFee;

  @Column
  private Double referFee;

  @Column
  private String currencyCode;

  @CreationTimestamp
  private Date createdAt;

  @UpdateTimestamp
  private Date updatedAt;

  @Id
  @OneToOne
  @JoinColumn(name = "product_id")
  @JsonBackReference
  private Product product;
}

我的问题是,在调用该端点时,即使我发送了实际数据,productCost 变量的所有字段都被设置为 null。

请求体如下:

{
   productCost: {
     createdAt: "2020-08-22T21:22:33.989+0000",
     currencyCode: "USD",
     fbaFee: 0,
     handling: 0,
     pickPack: 0,
     referFee: 0,
     shippingCost: 0,
     unitCost: 5,
     updatedAt: "2020-08-22T21:22:33.989+0000",
     weightHandling: 0
   }
}

我是否漏掉了明显的什么?为什么产品成本从请求的 body 正确地映射到控制器中的 productCost 变量?

英文:

I have the following Controller:

@Validated
@RestController
public class ProductController {
  @Autowired
  private ProductService productService;

  @PutMapping(&quot;/products/{productId}/costs&quot;)
  public ResponseEntity&lt;Product&gt; updateProductCost(@RequestHeader String authorization, @PathVariable UUID productId, @RequestBody ProductCost productCost) {
    Product updatedProduct = productService.updateProductCost(productId, productCost);

    return ResponseEntity.ok(updatedProduct);
  }
}

The ProductCost model looks like:

@Data
@NoArgsConstructor
@Entity
@Table(name = &quot;product_costs&quot;, schema = &quot;mws&quot;)
public class ProductCost implements Serializable {
  private static final long serialVersionUID = 1789128204447938816L;

  @Column
  private Double unitCost;

  @Column
  private Double shippingCost;

  @Column
  private Double pickPack;

  @Column
  private Double weightHandling;

  @Column
  private Double handling;

  @Column
  private Double fbaFee;

  @Column
  private Double referFee;

  @Column
  private String currencyCode;

  @CreationTimestamp
  private Date createdAt;

  @UpdateTimestamp
  private Date updatedAt;

  @Id
  @OneToOne
  @JoinColumn(name = &quot;product_id&quot;)
  @JsonBackReference
  private Product product;

My problem is that when calling that endpoint, the productCost variable comes with all fields set to null, even though I'm seding it real data.

The request body looks like:

{
   productCost: {
     createdAt: &quot;2020-08-22T21:22:33.989+0000&quot;
     currencyCode: &quot;USD&quot;
     fbaFee: 0
     andling: 0
     pickPack: 0
     referFee: 0
     shippingCost: 0
     unitCost: 5
     updatedAt: &quot;2020-08-22T21:22:33.989+0000&quot;
     weightHandling: 0
   }
}

Am I missing something obvious? Why is the Product Cost not mapped correctly from my request's body to the productCost variable in the controller?

答案1

得分: 2

只发送productCost的值。

{
     "createdAt": "2020-08-22T21:22:33.989+0000",
     "currencyCode": "USD",
     "fbaFee": 0,
     "handling": 0,
     "pickPack": 0,
     "referFee": 0,
     "shippingCost": 0,
     "unitCost": 5,
     "updatedAt": "2020-08-22T21:22:33.989+0000",
     "weightHandling": 0
}

因为您没有将productCost封装在任何其他类中以解析productCost键。

英文:

Send only value of productCost.

{
     createdAt: &quot;2020-08-22T21:22:33.989+0000&quot;
     currencyCode: &quot;USD&quot;
     fbaFee: 0
     andling: 0
     pickPack: 0
     referFee: 0
     shippingCost: 0
     unitCost: 5
     updatedAt: &quot;2020-08-22T21:22:33.989+0000&quot;
     weightHandling: 0
   }

Because you are not enclosing productCost in any other class to parse the productCost key.

huangapple
  • 本文由 发表于 2020年8月23日 06:19:52
  • 转载请务必保留本文链接:https://go.coder-hub.com/63541651.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定