[英]How to pass 2 objects in @RequestBody?
您能帮我解决通过@RequestBody传递两个对象的问题吗? 据我所知您不能传递2个@RequestBody参数,因此我创建了Tuple
类来存储自定义数据。 以我为例,我需要以JSON表示形式传递Book
对象和int值。 我已经尝试过不同的方法,但是每次都无法正确解析。
@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class Tuple<K, V> {
private K key;
private V value;
}
我在这种方法中使用Tuple
。
@PutMapping("action/returnBook")
public ResponseEntity<Void> returnBook(@RequestBody final Tuple<Long, Long> userIdBookInstanceId) {
leasingHistoryService.returnBook(userIdBookInstanceId.getKey(), userIdBookInstanceId.getValue());
return new ResponseEntity<>(HttpStatus.OK);
}
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class Book {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;
private String title;
@ManyToOne(cascade = CascadeType.ALL, optional = false)
private Author author;
}
@Entity
@NoArgsConstructor
@AllArgsConstructor
@Getter
@EqualsAndHashCode
@ToString
public final class Author {
@Id
@GeneratedValue(strategy=GenerationType.IDENTITY)
private Long id;
private String name;
private LocalDate dateOfBirth;
private String bio;
}
我应该在PUT
请求中传递的json的结构是什么?
我找到了一种方法。 在这种情况下,它是以下json:
{
"key" : {
"title": "The Girl in the Spider's Web v17",
"author": {
"id": 2,
"name": "Larsson",
"dateOfBirth": "1954-08-15",
"bio": "Author of the Millennium trilogy"
}
},
"value": 3
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.