英文:
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("/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?
答案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: "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.
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论