I was working with the same issue, I wanted to store multiple records at once.
After a lot of research, **I found an easy solution that worked immediately**.
**Before solving the problem code:**
**Controller method**
@PostMapping("/addList")
public ResponseEntity<List<Alarm>> saveAlarmList(@RequestBody List<AlarmDTO> dtos) {
return ResponseEntity.ok(alarmService.saveAlarmList(dtos));
}
**Service method**
public List<Alarm> saveAlarmList(List<AlarmDTO> dtos) {
List<Alarm> alarmList = new ArrayList<>();
for (AlarmDTO dto: dtos) {
Alarm newAlarm = AlarmDtoMapper.map(dto);
alarmList.add(newAlarm);
}
return alarmRepository.saveAll(alarmList);
}
**Entity class**
public class Alarm{
@Id
@Basic(optional = false)
@NotNull
@Column(name = "alarm_id")
private Long alarmId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "alarm_name")
private String alarmName;
}
**postman object**
[
{
"alarmId":"55",
"alarmName":"fgdffg",
},
{
"alarmId":"77788",
"alarmName":"hjjjjfk",
}
]
**and another format**
{
"list": [
{
"alarmId":"55",
"alarmName":"fgdffg",
},
{
"alarmId":"77788",
"alarmName":"hjjjjfk",
}
]
}
In both methods, it was giving an error of serialization/deserialization.
then I just **implemented the Serializable interface in the Entity class** as follows.
**Entity class**
public class Alarm implements Serializable
{
@Id
@Basic(optional = false)
@NotNull
@Column(name = "alarm_id")
private Long alarmId;
@Basic(optional = false)
@NotNull
@Size(min = 1, max = 100)
@Column(name = "alarm_name")
private String alarmName;
}
After this modification the first type of request object from postman worked.