public ResponseEntity<TastyDashResponse> order(@PathVariable("restaurantId") String restaurantId,
@RequestBody RestaurantOrderBook request,
@RequestBody ExpItems exp) {}
我有两个 Objects
RestaurantOrderBook
和 ExpItems
。传入请求应该有其中一个,另一个成为可选的。
如何在相同的方法中实现这一点。当这个 code
我得到 404 bad request
。请帮忙。
最佳答案
您不能使用两个@RequestBody,因为它可以绑定(bind)到单个对象。 解决方案创建一个对象,它将像这样捕获所有相关数据:
public class Data {
private String restaurantId;
private RestaurantOrderBook request;
private ExpItems exp;
getters/setters
}
public ResponseEntity<TastyDashResponse> order(@RequestBody Data data) {}
关于java - 如何根据 JSON 要求在 Spring REST Controller 中获取请求?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43739096/