C
C
Chvalov2018-10-10 01:35:34
Spring
Chvalov, 2018-10-10 01:35:34

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"
      }
    ]
  }
]

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:
Header Service
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 добавить сортировку или возможность сортировки
    }

    @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 above, I get the following error:
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 cascading data - then it works

Если отправлять вот такой json
{
    "name": "Accept-Language",
    "headerValuesEntityList": [
      {
        "value": "de"
      },
      {
        "value": "de-CH"
      },
      {
        "value": "en-US"
      }
    ]
  }
тогда все работает
how to send a bunch of values ​​instead of one feed ?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
L
LS Timer, 2018-10-10
@Chvalov

Try this

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

or accept as Hashmap or Object

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question