![](/img/trans.png)
[英]Send JSON body in HTTP GET request in java/spring boot
[英]How to get a Json from the request body and insert it in a spring boot relationship column?
我正在學習 spring 引導並嘗試在請求正文中發送 Json 並將其值插入具有關系的表中。 我有一張pets
桌和一張friends
桌。 一只寵物可以有很多朋友(其他寵物)。 基本思想是,當我在另一只寵物的個人資料上單擊“加我為friend me
”按鈕時,它會發送一個 json object,其中包含我的pet_id
和friend_id
,這樣我就可以使用類似SELECT * FROM friends WHERE pet_id = 123
的查詢來獲取朋友列表對於我的 ID 為 123 的寵物。
這是我的寵物 java
@Entity
@Table(name = "pets")
public class Pet {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pet_id", nullable = false)
private int petId;
@Column(name = "pet_name", nullable = false)
private String petName;
@Column(name = "species", nullable = false)
private String species;
@Column(name = "sex", nullable = false)
private String sex;
@Column(name = "breed", nullable = false)
private String breed;
@Column(name = "age", nullable = false)
private int age;
@Column(name = "pet_description", nullable = false)
private String petDescription;
@Column(name = "user_id", nullable = false)
private String userId;
@JsonIgnore
@OneToMany(fetch = FetchType.EAGER, cascade = CascadeType.ALL)
@JoinColumn(name = "foreign_key_column", referencedColumnName = "pet_id")
private List<Friend> friendsList = new ArrayList<>();
public Pet() {
}
public Pet(int petId, String petName, String species, String sex, String breed, int age, String petDescription, String userId) {
this.petId = petId;
this.petName = petName;
this.species = species;
this.sex = sex;
this.breed = breed;
this.age = age;
this.petDescription = petDescription;
this.userId = userId;
}
public List<Friend> getFriendsList() {
return friendsList;
}
這是我的朋友。java
@Entity
@Table(name = "friends")
public class Friend {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pet_friend_uid", nullable = false)
private int petFriendUid;
@Column(name = "friend_id")
private int friendId;
public Friend() {
}
public Friend(int friendId) {
this.friendId = friendId;
}
public int getFriendId() {
return friendId;
}
public int getPetFriendUid() {
return petFriendUid;
}
}
還有我的 FriendController.java
@RestController
@RequestMapping("api/friendslist")
public class FriendController {
public FriendService friendService;
public FriendController(FriendService friendService) {
this.friendService = friendService;
}
//build create new Friend API
@PostMapping
public ResponseEntity<Friend> saveFriend(@RequestBody Friend friend) {
ResponseEntity<Friend> newFriend = new ResponseEntity<Friend>(friendService.saveFriend(friend), HttpStatus.CREATED);
return newFriend;
}
@GetMapping
public List<Friend> getAllFriends() {
return friendService.getAllFriends();
}
}
我真的很難理解如何發送 Json,比方說 postman,帶有這樣的發布請求
{
"friendId": 111,
"foreign_key_column": 3
}
目前,如果我從 postman 發送上面的 json,只有friendId
被發布到表上,但引用父表的foreign_key_column
仍然是 null。是否需要將foreign_key_column
值作為路徑變量發送? 任何幫助是極大的贊賞。
Spring 使用jackson到 map JSON 字符串到 API 中定義的請求主體。Jackson 映射 json 字符串由 8842829 變量names, getters and setters
8958 變量名稱嘗試進行以下更改:
朋友.class
@Entity
@Table(name = "friends")
public class Friend {
@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
@Column(name = "pet_friend_uid", nullable = false)
private int petFriendUid;
@Column(name = "friend_id")
private int friendId;
public int getPetFriendUid() {
return petFriendUid;
}
public void setPetFriendUid(int petFriendUid) {
this.petFriendUid = petFriendUid;
}
public int getFriendId() {
return friendId;
}
public void setFriendId(int friendId) {
this.friendId = friendId;
}
}
從 postman 發送以下 json 字符串:
{
"friendId": 111,
"petFriendUid": 3
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.