Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to return a set of objects with Spring Boot?

I did a lesson about Spring Boot and it works perfectly. But what if I want to return a set of objects ? I tried doing this but it doesn't work. How can I do it correctly ?

With one object (it works):

@RequestMapping("/greeting")
public Greeting greeting(@RequestParam(value="name", defaultValue = "World") String name) {
    return new Greeting(counter.incrementAndGet(),
            String.format(template, name));
}

With many objects (it doesn't work):

@RequestMapping(value = "/greeting", method = RequestMethod.GET, consumes = MediaType.APPLICATION_JSON_VALUE, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Greeting> greeting() {
    Greeting greeting1 = new Greeting(1, "One");
    Greeting greeting2 = new Greeting(2, "Two");
    List<Greeting> list = new ArrayList<>();
    list.add(greeting1);
    list.add(greeting2);
    return list;
}
like image 425
faoxis Avatar asked Jan 18 '17 12:01

faoxis


People also ask

How do you use response entity?

ResponseEntity represents the whole HTTP response: status code, headers, and body. As a result, we can use it to fully configure the HTTP response. If we want to use it, we have to return it from the endpoint; Spring takes care of the rest.

What is the type of object returned by SpringApplication run APP class args?

Returns read-only ordered Set of the ApplicationListener s that will be applied to the SpringApplication and registered with the ApplicationContext .


2 Answers

If you compare your original method to your newly made one (with a List), you'll notice a few differences.

First of all, within the @RequestMapping annotation you're now using the properties consumes and produces. produces is not a problem here, because you are producing a response that should be JSON. However you're not consuming anything, so you should leave away the consumes.

@RequestMapping(value = "/greeting", method = RequestMethod.GET, produces = MediaType.APPLICATION_JSON_VALUE)
public @ResponseBody List<Greeting> greeting() {
    Greeting greeting1 = new Greeting(1, "One");
    Greeting greeting2 = new Greeting(2, "Two");
    List<Greeting> list = new ArrayList<>();
    list.add(greeting1);
    list.add(greeting2);
    return list;
}

As a sidenote, you might also notice that you used the @ResponseBody annotation. Putting it here won't cause any errors, but it is not necessary, because if you followed the Spring tutorial correctly, you should have annotated your controller with @RestController and by doing that, you already tell Spring that it will use a response body.

like image 99
g00glen00b Avatar answered Oct 27 '22 00:10

g00glen00b


Let Say we have list of CarDetails Pojo and we want to return them back

@RestController
public class CarDetailController {
  @GetMapping("/viewAllCarDetailList")
    public List<CarDetail> retrieveAllCarDetails() {
        List<CarDetail> contacts = new ArrayList<CarDetail>();

        CarDetail objt = new CarDetail();
        objt.setCarModel("hyundai");
        objt.setSubModel("I10");
        CarDetail objt2 = new CarDetail();
        objt2.setCarModel("hyundai");
        objt2.setSubModel("I20");        
        contacts.add(objt);
        contacts.add(objt2);
        return contacts;
    }
}
    public class CarDetails {

            private String carModel;
            private String subModel;
// Will haave Setter getter and hash code equls method
//and constructor
    }

This JSON will be output:-

[
    {
        "carModel": "hyundai",
        "subModel": "I10"
    },
    {
        "carModel": "hyundai",
        "subModel": "I20"
    }
]

output in Postman

like image 2
Vipul Gulhane Avatar answered Oct 27 '22 01:10

Vipul Gulhane