My request is ajax based and am calling to one of spring multiaction controller method,I able to create json file using Gson library. Is any way to return json view from controller method.
Asked
Active
Viewed 7,108 times
2 Answers
2
You can just let your method return the JSON String if you use the @ResponseBody
annotation and use one of the methods listed in the answers to my previous question:
In Spring MVC, how can I set the mime type header when using
@ResponseBody

Community
- 1
- 1

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588
-
Sean Patrick Floyd :I saw your solution how can i do samething without annotations. – user581805 Jan 24 '11 at 12:11
-
Very excellent solution,i will try this solution without annotations – user581805 Jan 24 '11 at 12:55
-
Sean Patrick Floyd :i didn't see CharacterStreams,MediaType classes in Spring 2.5,do i need to download any jars to implement your solution. – user581805 Jan 24 '11 at 16:16
-
No, my guess is that these types have been added in Spring 3.x. (I've never tried MVC in 2.5) – Sean Patrick Floyd Jan 24 '11 at 16:19
-
Thanks Sean,MediaType is from Spring 3.0,there i will mention application/json.But CharStreams and Closeables api is from google code.IS there any way to write same functionlity with native java code. – user581805 Jan 24 '11 at 17:13
-
Oh, you mean my own solution, sorry. Yes, of course, it just gets more verbose. `CharStreams.copy(CharStreams.newReaderSupplier(json), writer)` creates a `StringReader` from a String and copies all of it's characters to a `PrintWriter`. `Closeables.closeQuietly(writer)` is just a shortcut for calling close() on the writer and wrapping that in a try/catch/finally block. So yes, you can do all of that in plain Java. – Sean Patrick Floyd Jan 24 '11 at 17:18
-
Sean Patrick Floyd:how can i achieve samething with Spring webflow. – user581805 Jan 26 '11 at 17:44
0
I'm a total newbie but I've read some where that when you dont specify any logical view mapping for a controller and you return a model map from a handler method (@Controller
, @RequestMapping
) it should transform the model object to json and return it.

Sean Patrick Floyd
- 292,901
- 67
- 465
- 588

krsnik
- 69
- 1
- 1