|
|
|
|
|
JSON (JSON@TA) is a key component in Ajax and a REST framework like Jersey needs to support it well to be successful. Jakub reports on a new simpler default JSON data format in the latest Jersey; check his Description of Features and then see its use in jMaki Widgets. Check it out and let Jakub, Paul and Marc know how it works for you. |
Jersey's latest release is 0.6ea and is available from the Jersey Downloads Page. The latest stable release is 0.5ea and is also available through the Update Center. Jersey 0.6ea will be pushed to the UC in about 10 days.
|
A tip on jMaki and Jersey working together: How to consume JSON and XML representations generated by Jersey in jMaki ? |
|
Arun has started a TOTD (Tip of the Day) Series covering jRuby, jMaki, Metro, GlassFish and more. The entries are usually very detailed and focused on specific tasks. You can get the whole stream as an RSS Feed. The last 5 entries are: |
• Using JDBC connection pool/JNDI name from GlassFish in Rails Application,
• Generating JSON using JAXB and Jersey
• Switch between jRuby and cRuby in NetBeans
• Difference betwen Ruby Gems and Plugins
• Loading Data from Beans in jMaki Widgets
We will try to give you heads-up of new additions via quick Tips.
|
If you're new to JAX-RS (JSR 311) and RESTful Web Service with Java, wondering how it impacts you as a developer, you should read this introduction to the various Sun efforts in that space.
If you're tracking the progress of Jersey, the JAX-RS Reference Implementation, you'll probably interested to hear that version 0.2.1 has been released today with a documented list of required JAR dependencies, illustrated in this demo showing how JAXB beans can be serialized as JSON or XML depending on the |
At this point, installing in GlassFish is fairly easy with a jersey-on-glassfish.xml ANT script. It'll get even easier once Jersey is made available on the GlassFish Update Center. Any day now I hear ;)
|
|
Paul added support for pluggable type system in Jersey. All the supported types need to implement the EntityProvider interface and are registered in the META-INF/services file javax.ws.rs.ext.EntityProvider. Jakub used that system to add support for JSON encoding. Jersey is the GlassFish project that is implementing JAX-RS |
The JSON binding in JAX-RS enables to use application/json as
the MIME type and specify
JSONArray,
JSONObject as
method parameters and return type. The JSON binding in JAX-RS enables to write
code like the following:
@UriTemplate("/rates")
public class ExchangeRates {
@ProduceMime("application/json")
@UriTemplate("CZK")
public JSONObject getRates() {
try {
return new JSONObject()
.put("USD", 20.04);
} catch (JSONException ex) {
throw new WebApplicationException(ex);
}
}
}
JAX-WS takes advantage of a similar pluggable encoding layer in their implementation to provide a JSON binding.
More improvements to the JSON support in GlassFish. Kohsuke reports on progress in the client-side programming model, including a new, JavaScript-centric tool in the spirit of wsimport).
The JavaScript client can then just include the proxy script and then asynchronously invoke the WS service as follows:
<script src="path/to/endpoint?js"></script> myService.get( {id:5}, function(r) { alert("ID="+r.id); alert("title="+r.title); } );
|
The JSON extension exploits the Schema analysis machinery in JAX-WS which means that it can generate nice JavaScript and can also do things like generating very nice looking documentation automatically - as shown (somewhat washed out) at left. JSON support is in the "and more..." portion of Kohsuke and Jitu's presentation at JavaOne: TS-4948 "Unleashing the Power of JAX-WS RI: Spring, Stateful Web Services, SMTP, and More... :-). At least Kohsuke, and probably also Jitu, will also be present at GlassFish Day - register for free here. |
|
The JAX-WS project in GlassFish is taking advantage of the pluggable encoding layer in their implementation to provide a JSON binding. This Implementation is at an early stage and is part of the JAX-WS-commons extensions which also includes such committed extensions as: Spring Support, HTTP Session Scope Service, Thread Scope Service and the JAX-WS Maven 2 plugin. The JSON binding enables to write code like the following: |
@BindingType(JSONBindingID.JSON_BINDING) public class MyService { public Book get() { return new Book(); } public static final class Book { public int id = 1; public String title = "Java"; } }
|
It all started with Dave "Roller" Johnson writing about Roller generating JSON. Alexis picked it up in his jMakifying Roller entry and short video. Some limitations found in this quick hack were addressed in this follow-up entry and in the jMaki day presentations. Neat!! In another example combining JSON and jMaki Jennifer show how to load a table from JSON. Don't miss the comments for the entry, they are also useful. In many case XML is still a no-brainer, but the increased adoption of JavaScript on the client makes JSON an interesting lingua franca for web apps. And, in cases where JavaScript is also used on the server-side (e.g. Phobos) JSON seems the most natural format for structured data exchange. |
Via TheGalaxy, Daniel Rubio's recent article talks about the advantages of JavaScript Object Notation (JSON), and how JSON as an approach has recently gained popularity with Ajax services and Web-based clients. In this entry on TheServerSide, a discussion is open on Daniel's article. "What benefits have you been experiencing while using JSON? Do you agree that its recent boost in popularity has been due to Ajax?"