Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to get list of objects via requestbody in spring boot api

To get list of objects via @RequestBody in controller and process each object in a list to do a business logic.

I have tried this but not working

@RequestMapping(value="/updateservicetype", method=RequestMethod.POST,produces="application/json")
    public @ResponseBody ServiceTypesMessage updateServiceType(@RequestBody List<BarberServiceType> serviceTypes,final HttpServletResponse response){

also tried following:

@RequestMapping(value="/updateservicetype", method=RequestMethod.POST,produces="application/json")
    public @ResponseBody ServiceTypesMessage updateServiceType(@RequestBody BarberServiceType[] serviceTypes,final HttpServletResponse response){
like image 403
Hari Avatar asked Feb 08 '16 09:02

Hari


1 Answers

Below works for me

@RequestMapping(value = "/payments", method = RequestMethod.POST, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Payment> batchCreate(@RequestBody List<Payment> payments) {
  return paymentService.create(payments);
}

You will need Jackson in the class path

<dependency>
    <groupId>com.fasterxml.jackson.core</groupId>
    <artifactId>jackson-databind</artifactId>
    <version>2.6.0</version>
</dependency>

Json in put is

[{"sort":"10-20-30","account":"1234"},{"sort":"10-20-30","account":"1234"}]
like image 110
Essex Boy Avatar answered Sep 30 '22 13:09

Essex Boy