Deal with @ResponseBody in Spring mvc

Ways to resolve views in Spring MVC
  1. Return a String with the view name and add attributes to the Model or ModelMap
  2. Return a ModelAndView with the view name and model
  3. Return an Object with a @ResponseBody annotation -when we want to return json/xml
i.e. 3.
@ResponseBody -it is an Spring annotation that dealt with ajax response 
when we are using ajax call.

@Requestmapping(value="/states", method=RequestMethod.GET)
public @ResponseBody List<State> stateList() {
    return serviceImpl.getAllState();
}
 
OR
 
@RequestMapping(value="/states", method=RequestMethod.GET ,
 produces={"application/json","application/xml"})
@ResponseBody
public List<State> AllState() {
    return serviceImpl.getAllState();
} 

Comments

Popular posts from this blog

Angular 2 & 2+

How spring flow

database logic