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

Rahul Kumar :

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.

Optio :

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

Guess you like

Origin http://10.200.1.11:23101/article/api/json?id=461866&siteId=1