How should I structure a JSON POST, the controller on my backend, and the POJO classes?
This is for a twitter clone, so a user can have multiple tweets etc
POST
{
"tweet": "Sew one button, doesn't make u a tailor",
"user": "Alex"
}
Tweet Controller
public Tweet createTweet(@RequestBody Tweet tweet) {
return tweetRepository.save(tweet);
}
Tweet Class
@Table(name = "tweets")
public class Tweet {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
@ManyToOne(cascade = CascadeType.PERSIST)
private User user;
...
User Class
@Table(name = "custom_user")
public class User {
@Id
@GeneratedValue(strategy = GenerationType.AUTO)
private Long id;
private String name;
@OneToMany(cascade = CascadeType.ALL)
@JoinColumn(name = "tweet_id")
private List<Tweet> tweet = new ArrayList<>();
This is the response I'm getting
{
"id": 1,
"user": {
"id": 2,
"name": "Alex",
"tweet": []
},
"tweet": "Sew one button, doesn't make u a tailor"
}
edit:
If I do a GET on my users endpoint, this is what I get (there should be associated tweets included)
[
{
"id": 2,
"name": "Alex",
"tweet": []
}
]