I would decide to reorganize the service to return a domain object, not JSON strings, and let Spring handle serialization (via MappingJacksonHttpMessageConverter when writing). Starting with Spring 3.1, the implementation looks pretty neat:
@RequestMapping(produces = MediaType.APPLICATION_JSON_VALUE, method = RequestMethod.GET value = "/foo/bar") @ResponseBody public Bar fooBar(){ return myService.getBar(); }
Comments:
Firstly, <mvc:annotation-driven /> or @EnableWebMvc should be added to your application configuration.
The produces attribute of @RequestMapping annotations is @RequestMapping used to indicate the type of response content. Therefore, it must be set to MediaType.APPLICATION_JSON_VALUE (or "application/json" ).
Finally, Jackson should be added so that any serialization and de-serialization between Java and JSON will be handled automatically by Spring (Jackson's dependency is determined by Spring, and MappingJacksonHttpMessageConverter will be under the hood).
matsev Nov 24 '12 at 16:28 2012-11-24 16:28
source share