Skip to content
Advertisement

SpringBoot ignoring my @RequestBody param?

I have the following Controller:

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

The ProductCost model looks like:

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

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: "2020-08-22T21:22:33.989+0000"
     currencyCode: "USD"
     fbaFee: 0
     andling: 0
     pickPack: 0
     referFee: 0
     shippingCost: 0
     unitCost: 5
     updatedAt: "2020-08-22T21:22:33.989+0000"
     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?

Advertisement

Answer

Send only value of productCost.

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

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

User contributions licensed under: CC BY-SA
3 People found this is helpful
Advertisement