Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

JSON formatting view for Spring MVC [SPR-5853] #10522

Closed
spring-projects-issues opened this issue Jun 28, 2006 · 21 comments
Closed

JSON formatting view for Spring MVC [SPR-5853] #10522

spring-projects-issues opened this issue Jun 28, 2006 · 21 comments
Assignees
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Milestone

Comments

@spring-projects-issues
Copy link
Collaborator

Andres Almiray opened SPR-5853 and commented

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.


Attachments:

Issue Links:

  • SWF-1273 Json-Rpc-Java (AJAX) and Spring integration
  • SWF-1295 Move the JacksonJsonView implementation to the core Spring Framework

Referenced from: commits 5ae919a, 398729c

26 votes, 26 watchers

@spring-projects-issues
Copy link
Collaborator Author

Andres Almiray commented

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

@spring-projects-issues
Copy link
Collaborator Author

Rob Harrop commented

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

@spring-projects-issues
Copy link
Collaborator Author

Andres Almiray commented

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

@spring-projects-issues
Copy link
Collaborator Author

Rogier Goede commented

json-lib implementation of a ServiceExporter with dojo support

@spring-projects-issues
Copy link
Collaborator Author

Rogier Goede commented

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

@spring-projects-issues
Copy link
Collaborator Author

Andres Almiray commented

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).

@spring-projects-issues
Copy link
Collaborator Author

Julian Dreißig commented

In renderMergedOutputModel, you have to actually set the contentType of the response, like

response.setContentType(this.getContentType());

@spring-projects-issues
Copy link
Collaborator Author

Condor commented

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.

@spring-projects-issues
Copy link
Collaborator Author

Andres Almiray commented

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

@spring-projects-issues
Copy link
Collaborator Author

Condor commented

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()).

@spring-projects-issues
Copy link
Collaborator Author

Andres Almiray commented

Update with latest suggestions from M.H. Avegaart
Added javadocs too :-)

@spring-projects-issues
Copy link
Collaborator Author

Domenico Testa commented

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.

@spring-projects-issues
Copy link
Collaborator Author

Domenico Testa commented

View updated to support json-lib 2.1

@spring-projects-issues
Copy link
Collaborator Author

Condor commented

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);
}

@spring-projects-issues
Copy link
Collaborator Author

Andres Almiray commented

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.

@spring-projects-issues
Copy link
Collaborator Author

Chad Shryock commented

How does one use this? Is there documentation anywhere?

@spring-projects-issues
Copy link
Collaborator Author

Lari Hotari commented

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).

@spring-projects-issues
Copy link
Collaborator Author

Andres Almiray commented

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.

@spring-projects-issues
Copy link
Collaborator Author

Paul Sundling commented

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?

@spring-projects-issues
Copy link
Collaborator Author

Kirill Kovalchuk commented

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

@spring-projects-issues
Copy link
Collaborator Author

Brian C. Dilley commented

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 :)

@spring-projects-issues spring-projects-issues added type: enhancement A general enhancement has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: web Issues in web modules (web, webmvc, webflux, websocket) labels Jan 11, 2019
@spring-projects-issues spring-projects-issues added this to the 3.0 M4 milestone Jan 11, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
has: votes-jira Issues migrated from JIRA with more than 10 votes at the time of import in: web Issues in web modules (web, webmvc, webflux, websocket) type: enhancement A general enhancement
Projects
None yet
Development

No branches or pull requests

2 participants