I am working on a Spring 3 project and trying to write RESTful services.
I have problems with getting the object list from the server. Here is the server code:
@RequestMapping(value = "/getRestItemList", method = RequestMethod.GET)
public ModelAndView getRestItemList() {
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<String> entity = new HttpEntity<String>(headers);
RestItemCollection itemList = new RestItemCollection();
try {
ResponseEntity<RestItemCollection> forObject = restTemplate.exchange("http://localhost:3080/SOHOV01R01C01/getRestItemList",
HttpMethod.GET, entity,
RestItemCollection.class);
itemList = forObject.getBody();
} catch (Exception e) {
System.out.println(e.getMessage());
}
}
And the client code:
@RequestMapping(method = RequestMethod.GET, value = "/getRestItemList", headers = "Accept=application/xml, application/json")
public @ResponseBody
List<RestItem> getRestItemList() {
List<RestItem> itemList = restManService.getRestItemList();
return itemList;
}
When I debug, I can get the object list in the server side. But when it is being sent, some problem raises.
Where am I wrong?
What i understand getRestItemList() expected to return json,İf so you should annotate this metod with @ResponseBody (this annotation tell spring to return result as json and not try to solve a view) .In fact the client code: looks right but server code meaningless.