I want to post a subject and it has a promotorList and a targetAudienceList.
when i get the requestbody as a string i can print this on java side:
{
"title": "qsdgqsg",
"description": "qdfgqdg",
"amountOfStudents": "1",
"targetAudienceList": [
{
"targetAudience": {
"targetAudienceId": 1
}
},
{
"targetAudience": {
"targetAudienceId": 2
}
}
]
"promotorList": [
{
"promotor": {
"promotorId": 1
}
}
]
}
but when i take the requestbody as a subject object in java like this:
@PostMapping(path = "/create")
public void createSubject(@RequestBody Subject subject) {
System.out.println(subject);
//subjectService.addSubject(subject);
}
i get this if i print it:
Subject(subjectId=null, title=qsdgqsg, description=qdfgqdg, amountOfStudents=1, promotorList=[Promotor(promotorId=null, user=null, researchGroup=null)], topicList=null, targetAudienceList=[TargetAudience(targetAudienceId=null, majorCode=null, campus=null), TargetAudience(targetAudienceId=null, majorCode=null, campus=null)])
as you can see java makes entries for 2 targetaudiences in the list, but the id's are null for some reason.
Subject.java:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class Subject {
@Id
@SequenceGenerator(
name = "subject_sequence",
sequenceName = "subject_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "subject_sequence"
)
private Long subjectId;
private String title;
private String description;
private int amountOfStudents;
@ManyToMany(
cascade = CascadeType.ALL
)
@JoinTable(
name = "subject_promotor",
joinColumns = @JoinColumn(
name = "subject_id",
referencedColumnName = "subjectId"
),
inverseJoinColumns = @JoinColumn(
name = "promotor_id",
referencedColumnName = "promotorId"
)
)
private List<Promotor> promotorList;
@ManyToMany(
cascade = CascadeType.ALL
)
@JoinTable(
name = "subject_topic",
joinColumns = @JoinColumn(
name = "subject_id",
referencedColumnName = "subjectId"
),
inverseJoinColumns = @JoinColumn(
name = "topic_id",
referencedColumnName = "topicId"
)
)
private List<Topic> topicList;
@ManyToMany(
cascade = CascadeType.ALL
)
@JoinTable(
name = "subject_targetAudience",
joinColumns = @JoinColumn(
name = "subject_id",
referencedColumnName = "subjectId"
),
inverseJoinColumns = @JoinColumn(
name = "targetAudience_id",
referencedColumnName = "targetAudienceId"
)
)
private List<TargetAudience> targetAudienceList;
TargetAudience.java:
@Entity
@Data
@AllArgsConstructor
@NoArgsConstructor
public class TargetAudience {
@Id
@SequenceGenerator(
name = "targetAudience_sequence",
sequenceName = "targetAudience_sequence",
allocationSize = 1
)
@GeneratedValue(
strategy = GenerationType.SEQUENCE,
generator = "targetAudience_sequence"
)
private Long targetAudienceId;
// for example IW E-ICT-> industriele wetenschappen Elektronica ICT
private String majorCode;
@OneToOne(
cascade = CascadeType.ALL,
fetch = FetchType.EAGER,
optional = true
)
@JoinColumn(
name = "campus",
referencedColumnName = "name"
)
private Campus campus;
}
the solution might be obvious, but i'm new to spring boot and would appreciate an answer. Thank's in advance!
CodePudding user response:
Your request JSON is not matching your datamodel. The correct JSON for the datamodel you have would be:
{
"title": "qsdgqsg",
"description": "qdfgqdg",
"amountOfStudents": 1,
"targetAudienceList": [
{
"targetAudienceId": 1
},
{
"targetAudienceId": 2
}
],
"promotorList": [
{
"promotorId": 1
}
]
}
When the objectMapper of Spring cant find a variable it will set it to null. Since your request is missing the values in the correct places spring identifies thos as missing. Additional data that is not needed in the restObject in your Controller is ignored.
I would also advise yout that you do not directly use Entitys in RestConttrollers. You should use DTOs --> https://softwareengineering.stackexchange.com/questions/171457/what-is-the-point-of-using-dto-data-transfer-objects.