I have the following code as Spring Controller:
@RequestMapping(value = "/city/{city}")
public ModelAndView cityRestaurants(@PathVariable String city){
ModelAndView mav = new ModelAndView();
mav.setViewName("restaurant");
List<Restaurant> list = null;
try {
list = rService.getRestaurantsFromCity(city);
} catch (SQLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
mav.addObject("list", list);
return mav;
}
How can I access "list" on client side using AngularJS?
It's possible to define a controller that does not have a view but renders the return parameters as JSON. In order for this to work the Jersey library needs to be on the classpath:
@Responsebody
@RequestMapping(method=RequestMethod.GET, value = "/city/{city}", produces = "application/json" )
public List<Restaurant> cityRestaurants(@PathVariable String city){
return rService.getRestaurantsFromCity(city);
}
You can already test this service with something like Postman and see that it's working, then on the client side we can call this service using an Ajax request:
$http({
method: 'GET',
url: 'https://www.example.com/city/yourcityId',
}).success(function(data){
alert('restaurants: ' + data);
}).error(function(){
alert("error");
});
This should print the cities restaurants.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With