Hari February 2016

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){

Answers


Essex Boy February 2016

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

Post Status

Asked in February 2016
Viewed 2,614 times
Voted 6
Answered 1 times

Search




Leave an answer