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

2

10

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){
Lurdan answered 8/2, 2016 at 9:35 Comment(3)
Did you try your code with some REST client, there are many add-ons. I think you are sending something in wrong manner as Arrays surely work, and I have tried myself. Enable debug logging and without an error, it's very difficult to find what's really going on.Ball
"status":400,"error":"Bad Request","exception":"org.springframework.http.converter.HttpMessageNotReadableException","message":"Could not read JSON: Can not deserialize instance of com.lob.domain.BarberServiceType[] out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@6eda930c; line: 1, column: 1]; nested exception is com.fasterxml.jackson.databind.JsonMappingException: Can not deserialize instance of com.lob.domain.BarberServiceType[] out of START_OBJECT token\n at [Source: java.io.PushbackInputStream@6eda930c; line: 1, column: 1]","path":"/lob/updateservicetype"},Lurdan
Don't add error in comments, edit your main post and add it there, the complete log.Ball
M
26

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"}]
Morrison answered 8/2, 2016 at 10:50 Comment(2)
is it the right way of doing it?Selfservice
@PramodyaMendis I think so & the community seem to agree.Morrison
D
2

You should use a wrapper class for your required list, in another word, create a new class and name it "BarverServiceTypeRequest" this class should contain your List:

public class BarberServiceTypeRequest{
    private List<BarberServiceType> serviceTypes;
    public List<BarberServiceType> getserviceTypes() {
        return serviceTypes;
    }

    public void setServiceTypes(List<BarberServiceType>serviceTypes) {
        this.date = date;
    }

then your controller will be like:

@RequestMapping(value="/updateservicetype", method=RequestMethod.POST,produces="application/json")
    public @ResponseBody ServiceTypesMessage updateServiceType(@RequestBody BarberServiceTypeRequest request, final HttpServletResponse response){

Your JSON object will be like:

{
   "serviceTypes": [{"sort":"10-20-30","account":"1234"},{"sort":"10-20-30","account":"1234"}]
}

sure you can access your list with:

request.getserviceTypes();
Dmitri answered 14/6, 2022 at 14:24 Comment(1)
It's not a list. It's a map with a list.Linoel

© 2022 - 2024 — McMap. All rights reserved.