Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to add success/error flag while returning list of object as a response

enter image description hereenter image description here

 @RequestMapping(value = "/SubmitStep1.json", method = RequestMethod.POST,  headers = "Accept=application/json,application/xml")
        @ResponseBody
        public List<ShopDetails> showShopList(@RequestBody ShopDetails shopDetails)throws Exception{
            List<ShopDetails> shopDetailsList=new ArrayList<ShopDetails>();
            shopDetailsList=dbq.getShopDetails(shopDetails);
            return shopDetailsList;
        }

Here in above code i am returning list of shops which consist details of every shop as per locality.

So, my question is , can i add a success/error message while returning if i get the list of shops.

like image 410
Rahul Kumar Avatar asked Aug 21 '17 14:08

Rahul Kumar


1 Answers

As said @araknoid - you can create wrapper:

public class ShopListResponse {

private List<ShopDetails> shopList;

private String message;

public ShopListResponse (List<ShopDetails> shopList, String message){
this.shopList = shopList;
this.message = message;
}

// getters and setters
}

In your controller class:

@RequestMapping(value = "/SubmitStep1.json", method = RequestMethod.POST,  headers = "Accept=application/json,application/xml")
@ResponseBody
public ResponseEntity<ShopListResponse> showShopList(@RequestBody ShopDetails shopDetails)throws Exception{

List<ShopDetails> shopDetailsList = dbq.getShopDetails(shopDetails);
return new ResponseEntity<>(new ShopListResponse(shopDetailsList, "Success or error message"), HttpStatus.OK); 
}

If you want return an error - you can return HttpStatus.NOT_FOUND, or just return HttpStatus.OK and message with error - it's depends on your approach.

Here controller result: enter image description here

like image 56
Optio Avatar answered Oct 31 '22 22:10

Optio