Spring MVC returning JSONS and exception Handling?

Create a response class: public class Response { T data; boolean status = true; public Response(T d) { data = d; } } Then return that from your controllers: @ResponseBody public Response getUserDetails) { //... return new Response(userDetails); } For the exception you'll want to return an object like: public class BadStatus { String errorMessage; boolean status = false; public BadStatus(String msg) { errorMessage = msg; } } @ExceptionHandler(Exception. Class) public BadStatus handleException(Exception ex, HttpServletRequest request) { return new BadStatus(ex.getMessage()); }.

See this thread for a more detailed answer: stackoverflow. Com/questions/5641091/… – Sydney Apr 13 at 15:47.

Yes. Return a model and a view instead. Public ModelMap getUserDetails() { UserDetails userDetails; // get this object from somewhere ModelMap map = new ModelMap()(; map.

AddAttribute("data", userDetails); map. AddAttribute("success", true); return map; } To add the exception you'd do it the same way with a key and success = false.

I don't see what value ModelAndView adds here. Why not just return the ModelMap? – skaffman Mar 22 at 19:50 I am unsure of why I would want to use a ModelAndView however you gave me the idea of using a map (HashMap) which worked like a charm and gave me the desired output.

However how do I do the same in a fail scenario? However any idea of a way to centralize this when for all controllers and all functions in the controllers? – MilindaD Mar 22 at 20:24 @skaffman You're right.

Brain fart from returning views as well. @milindaD ModelMap doesn't require keys but not much different. It implements Map.

– Robby Pond Mar 22 at 20:27.

I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.

Related Questions