How to pass JsonArray to spring boot JPA?

Is that send JSON POST request:
[
{
 "name": "Accept-Language",
 "headerValuesEntityList": [
{
 "value": "de"
},
{
 "value": "de-CH"
},
{
 "value": "en-US"
}
]
},
{
 "name": "Cookie",
 "headerValuesEntityList": []
},
{
 "name": "Trailer",
 "headerValuesEntityList": [
{
 "value": "Expires"
}
]
}
]


Controller
@PostMapping("/header/new")
 public boolean save(@RequestBody HeaderEntity headerEntity) {
headerService.save(headerEntity);
 return true;
 }
Entity:
Header
@Entity
@Table(name = "header")
public class Header {

@Id
@GeneratedValue(strategy = GenerationType.IDENTITY)
private Long id;

@Column(length = 128, nullable = false, unique = true)
private String name;

@Column(length = 512)
private String description;

@OneToMany(cascade = CascadeType.ALL)
private List headerValuesList;
HeaderValues
@Entity
@Table(name = "header_value")
public class HeaderValuesEntity {

@Id
 @GeneratedValue(strategy = GenerationType.IDENTITY)
 private Long id;

 @Column(length = 512)
 private String value;

 @Column(length = 512)
 private String description;

 @ManyToOne(fetch = FetchType.LAZY)
 @JoinColumn(name = "header_id")
 private Header header;

 public HeaderValues(){
 }
Service:
HeaderService
public interface HeaderService {

 List<HeaderEntity> findAll();
 List<HeaderEntity> findByNameContaining(String name);

 boolean save(HeaderEntity headerEntity);
}
HeaderServiceImpl
@Service
public class HeaderServiceImpl implements HeaderService {

 private HeaderRepository headerRepository;

@Autowired
 public HeaderServiceImpl(HeaderRepository headerRepository){
 this.headerRepository = headerRepository;
}

 public List<HeaderEntity> findAll() {
 return headerRepository.findAll(); // todo add sorting or sorting
}

@Override
 public List<HeaderEntity> findByNameContaining(String name){
 return headerRepository.findByNameContaining(name);
}


 public boolean save(Header header) {
 if (headerEntity == null) return false;
headerRepository.save(header);
 return true;
}

}
When sending Json and above that get this error message:
JSON parse error: 'Cannot deserialize instance of `xxx.model.autocomplete.Header` out of START_ARRAY token; nested exception is com.fasterxml.jackson.databind.exc.MismatchedInputException: Cannot deserialize instance of `xxx.model.autocomplete.Header` out of START_ARRAY token\n at [Source: (PushbackInputStream); line: 1, column: 1]


If only cascade data - then it works

If you send this json here
{
 "name": "Accept-Language",
 "headerValuesEntityList": [
{
 "value": "de"
},
{
 "value": "de-CH"
},
{
 "value": "en-US"
}
]
 }
then it works
how to send a stack of values instead of just one feed ?
March 12th 20 at 08:04
1 answer
March 12th 20 at 08:06
Solution
Try this

@PostMapping("/header/new")
 public boolean save(@RequestBody List<HeaderEntity> headerEntityList) {
 for (HeaderEntity headerEntity: headerEntityList) {
headerService.save(headerEntity);
}

 return true;
 }


or take it as a Hashmap or Object

Find more questions by tags Spring