WASEEM
WASEEM

Reputation: 217

How to send list of entity class as response in spring

I have 5 entity class in spring. I have to encapsulate these all class in one class and collect all the values of that class in spring controller and send response.

If you have any idea please let me know

Upvotes: 0

Views: 991

Answers (2)

Ramesh Fadatare
Ramesh Fadatare

Reputation: 589

Create a new class like A and declare all 5 classes in this class like sample :

public class Response {
   private A a;
   private B b;
   private C c;
   private D d;
   private E e;

   // getter and setters
}

Fill above response object and return from the controller like

@RequestMapping(value="{id}", method = RequestMethod.GET)
	public @ResponseBody Response getShopInJSON(@PathVariable String id) {

		Response response = new Response();
    
    // add A,B,C,D,E to response object
		response.setA();
    response.setB();
    response.setC();
    response.setD();
    response.setE();
   		return response;

  	}

@ResponseBody annotation will convert the response as json internally using message converters

Upvotes: 1

Chaitanya
Chaitanya

Reputation: 131

Create a new entity which contains the other entity references as fields

public class MyResponse { private Entity1 entity1; }

Include all 5 entity as fields in MyResponse class and set the entities accordingly and then return MyResponse directly or in a response entity as ResponseEntity.ok(myResponse)

Upvotes: 1

Related Questions