I have four tables:
car
seat
tire
sound_player
I would like to know if using Spring REST, it is possible to write via POST a complete JSON, containing the objects of various models, without having to send the individual object of each model.
An example of a complete JSON would be as follows:
{
"color": "white",
"manufacturer": "toyota",
"model": "corolla",
"seat": {
"name": "Recaro",
},
"tire": {
"name": "Recaro",
"circlet": "18",
},
"soundPlayer": {
"name": "Sony DHC-5000",
}
}
You can create some DTO which will contain all models you need with all parameters
You can read about dto pattern by this link:
https://www.tutorialspoint.com/design_pattern/transfer_object_pattern.htm
In you case you can create :
@Getter
@Setter
public class CarDto{
private String color;
private String manufacturer;
private String model;
private SeatDto seat;
private TireDto tire;
private SoundPlayerDto soundPlayer;
}
@Getter
@Setter
public class SeatDto {
private String name;
}
@Getter
@Setter
public class TireDto{
private String name;
private String circlet
}
@Getter
@Setter
public class SoundPlayerDto{
private String name;
private String circlet
}
And you can pass your request as you described:
@RequestBody CarDto carDto;
Then you can write your own converters from dto to you real entities and work with them
@Getter and @Setter its lombok annotation you can read about them from following link: