Issue Details (XML | Word | Printable)

Key: SPR-5853
Type: New Feature New Feature
Status: Resolved Resolved
Resolution: Fixed
Priority: Minor Minor
Assignee: Arjen Poutsma
Reporter: Andres Almiray
Votes: 26
Watchers: 25
Operations

If you were logged in you would be able to see more operations.
Spring Framework

JSON formatting view for Spring MVC

Created: 28/Jun/06 11:47 AM   Updated: 24/Jun/09 09:04 AM
Component/s: SpringWEB
Affects Version/s: None
Fix Version/s: 3.0 RC1

Time Tracking:
Original Estimate: 3d
Original Estimate - 3d
Remaining Estimate: 3d
Remaining Estimate - 3d
Time Spent: Not Specified
Remaining Estimate - 3d

File Attachments: 1. Java Source File JsonView.java (3 kB)
2. Zip Archive JsonView.zip (46 kB)
3. Java Source File SmdServiceExporter.java (5 kB)
4. Zip Archive spring-jsonview.v2.1.zip (87 kB)
5. Zip Archive spring-jsonview.v2.zip (87 kB)
6. Java Archive File SpringJSONExporter.jar (25 kB)
7. File SpringJSONRPC.war (303 kB)

Issue Links:
Related
 


 Description  « Hide
Create a view that outputs its model as a JSON object. This will help AJAX enabled applications to transfer less bytes as the JSON format is more compact that XML. It also allows for quick object creation using eval(), no need for extra XML parsing.

I have created such view (JsonView) using the json-simple library (http://json.org/java) and would like to contribute it.



 All   Comments   Work Log   Change History   FishEye   Builds      Sort Order: Ascending order - Click to sort in descending order
Andres Almiray added a comment - 02/Jul/06 02:29 PM
Maven project with JsonView + tests. Includes the following dependencies:
json-lib – transforms model to JSON – http://json-lib.sourceforge.net
jstester – tests javaScript code inside java – http://jstester.sourceforge.net

Rob Harrop added a comment - 11/Jul/06 06:16 AM
Very interesting. I did the exact same thing recently as part of a prototype. I hope to get something like this in Spring 2.1 along with a JsonServiceExporter.

Rob


Andres Almiray added a comment - 23/Jul/06 11:51 AM
Rob, json-lib transforms beans, maps, collections and arrays to JSON format, but the current development snapshot can transform JSON to DynaBeans, collections, arrays and beans (althought the primitive array conversion still needs some work).
So if the intent of JsonServiceExporter is to mirror the other service exporters (client-server with two-way encoding/decoding) I think json-lib can help you accomplish that goal.

Andres


Rogier Goede added a comment - 01/Jan/07 01:53 PM
json-lib implementation of a ServiceExporter with dojo support

Rogier Goede added a comment - 01/Jan/07 01:55 PM
I was exactly on that track when I wrote the inappropriately named SmdServiceExporter.
This also exposes the service description in SMD format so that it can be accessed from a dojo client with only a few keystrokes:

var testClass = new dojo.rpc.JsonService("/dojo_test_server/remoting/TestService");
testClass.mapMethod("key",8).addCallbacks(mapContentCallBack,contentErrBack);

This is enough to call a method on a server side bean and pass the result to the mapContentCallBack function.
It would be great to have something like this in spring 2.1
If you are interested, I would be happy to tidy up this implementation and add things like

  • limited overloading support (based on the number of parameters)
  • a ProxyFactoryBean

Andres Almiray added a comment - 16/Jan/07 01:19 PM
I think that we have three different issues mixed, let me explain,
1. allow for a Spring MVC view to export/render its model as JSON (this is solved by the JSONView)
2. a Spring Remoting solution that uses json-rpc as its communication protocol (think HessianProxyFactoryBean -> JsonRPCProxyFactoryBean), this is solved by the Spring JSONExporter
3. integration of an AJAX toolkit and Spring beans, the beans will respond to calls made by the XHR. There is already an ongoing effor in Spring Modules's XT framework on this, but it is not based in the DOJO toolkit (as version 0.6).

Julian Dreißig added a comment - 26/Jan/07 06:17 AM
In renderMergedOutputModel, you have to actually set the contentType of the response, like

response.setContentType(this.getContentType());


M.H. Avegaart added a comment - 30/Jan/07 08:30 AM
I would like to propose some changes to JsonView:

1. Change the default content type to "application/json" (see RFC 4627).
2. Create the JSON object in a separate method (so it can be overridden in a subclass).
3. Add excludes and ignoreDefaultExcludes properties for use in the JSONObject.fromObject call.


Andres Almiray added a comment - 30/Jan/07 09:47 AM
Updated code to maven2, json-lib-1.0b2, ezmorph-1.0, jstester-1.3.
Added changes proposed by Julian Dreißig and M.H. Avegaart

M.H. Avegaart added a comment - 30/Jan/07 10:09 AM
Wow, that was quick update !

Some notes on JsonView v2:

1. Please add an overloaded version of the createJSON method that has request and response parameters (e.g. for access to session data).
2. You should probably replace response.getWriter().write(json.toString()) with json.write(response.getWriter()).


Andres Almiray added a comment - 30/Jan/07 11:14 AM
Update with latest suggestions from M.H. Avegaart
Added javadocs too

Domenico Testa added a comment - 20/Dec/07 12:21 PM
I've noticed the use of an old version of the json-lib (the stable version at the time of writing is 2.1).
I wrote an updated version of the view, using the new 2.x syntax for the configuration (exported as a bean property).

Watch at the attached file JsonView.java.


Domenico Testa added a comment - 20/Dec/07 12:22 PM
View updated to support json-lib 2.1

M.H. Avegaart added a comment - 16/Jan/08 07:22 AM
I suggest adding an extra method between renderMergedOutputModel and createJSON that allowes for overriding when JSON output is written directly to the response (e.g. when using net.sf.json.util.JSONBuilder).

Change:

protected void writeJSON(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
JSON json = createJSON(model, request, response);
json.write(response.getWriter());
}

protected void renderMergedOutputModel(Map model, HttpServletRequest request, HttpServletResponse response) throws Exception {
response.setContentType(getContentType());
writeJSON(model, request, response);
}


Andres Almiray added a comment - 20/Jan/08 03:42 PM
I decided to release JsonView as an extension of Json-lib. If anyone is interested the package is available at
http://sourceforge.net/project/showfiles.php?group_id=171425

M.H. Avegaart: I included your latest suggestion too.


Chad Shryock added a comment - 21/May/08 10:36 AM
How does one use this? Is there documentation anywhere?

Lari Hotari added a comment - 21/May/08 12:21 PM
Please checkout the features in http://spring-json.sourceforge.net/ . Besides json view, it also supports AJAX form validation and submission using spring mvc. It would be nice to get these features in standard Spring MVC. Spring-Json works well with ExtJS (with a little tweaking).

Andres Almiray added a comment - 21/May/08 12:56 PM
Speaking for the JsonView code attached to this issue (latest version [1.0.1] available at http://sourceforge.net/projects/json-lib) you may configure it as you would with any other SpringMVC view. It also accepts a JsonConfig bean to further customize how beans must be serialized.
JsonView is responsible for exporting data as JSON only (it is a view after all), any AJAX integration must be handled by your own components, thus giving full freedom to choose what makes more sense for your app.

Paul Sundling added a comment - 05/Feb/09 06:53 PM
I see there is also a project that seems to solve this called spring-json http://spring-json.sourceforge.net/ which supports both sojo and json-lib.

Which implementation, or yet a third solution will be included in Spring 3?


Kirill Kovalchuk added a comment - 10/May/09 08:25 AM
Rogier, what is the status for SmdServiceExporter?

I'm searching scalable, supportable solution for my projects to build a bridge between Browser and Spring's Services - I just want to be able to call remote method using Javacript and searching for an appropriate ServiceExporter.

In this issue we can see two solutions:

1) SmdServiceExporter
Client javascript library: DOJO
Service description: SMD format
Data transfer protocol: JSON-RPC
Server-side library: json-lib

2) JSONRPCServiceExporter.
Client javascript library: prototypejs
Service description: generated javascript code using prototypejs library
Data transfer protocol: JSON-serialized org.springframework.remoting.support.RemoteInvocation class
Server-side library: jsonrpc

Advantages of SmdServiceExporter:

  • don't stick to exact javascript library on the client
  • known formats are used (SMD and JSON-RPC)
  • small size of source code

Disadvantages of SmdServiceExporter:

  • no samples
  • no unit tests
  • no support for clients written in plain java

Advantages of JSONRPCServiceExporter:

  • unit tests
  • support for clients written in plain java+spring

Disadvantages of JSONRPCServiceExporter:

  • too much javascript code (because of using poor prototypejs)
  • stick to prototypejs while getting service description
  • using "java-like" data transfer protocol style tied to RemoteInvocation class

Conclusion: I think SmdServiceExporter is better than JSONRPCServiceExporter for a long perspective because:

  • dojo library is much better in use than prototypejs (avoid looking at UI and so on)
  • SmdServiceExporter uses more standards than JSONRPCServiceExporter

But I found some difficulties compiling SmdServiceExporter with latest versions of dojo and json-lib and I think that it doesn't support complex types. So if Rogier would share us updated version it would be appreciated.

BR
Kirill


Brian C. Dilley added a comment - 10/Jun/09 12:42 PM
I've created a project for a springframework JSON-RPC based RemoteExporter at the following URL:

http://code.google.com/p/spring-jsonrpc/

It's based on the specification being written at:

http://groups.google.com/group/json-rpc

note: The code for this project is not the same code attached to this issue - it's completely different. I just figured I'd let everyone know that it exists