Answer the question
In order to leave comments, you need to log in
How to pass JsonArray to spring boot JPA?
There is a JSON that I send with a POST request:
[
{
"name": "Accept-Language",
"headerValuesEntityList": [
{
"value": "de"
},
{
"value": "de-CH"
},
{
"value": "en-US"
}
]
},
{
"name": "Cookie",
"headerValuesEntityList": []
},
{
"name": "Trailer",
"headerValuesEntityList": [
{
"value": "Expires"
}
]
}
]
@PostMapping("/header/new")
public boolean save(@RequestBody HeaderEntity headerEntity) {
headerService.save(headerEntity);
return true;
}
@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(){
}
public interface HeaderService {
List<HeaderEntity> findAll();
List<HeaderEntity> findByNameContaining(String name);
boolean save(HeaderEntity headerEntity);
}
@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 добавить сортировку или возможность сортировки
}
@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;
}
}
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]
{
"name": "Accept-Language",
"headerValuesEntityList": [
{
"value": "de"
},
{
"value": "de-CH"
},
{
"value": "en-US"
}
]
}
тогда все работаетAnswer the question
In order to leave comments, you need to log in
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question