Skip to main content

JAX-WS RI extension for JSON

Posted by kohsuke on April 27, 2007 at 3:42 PM PDT

Jitu and I have been working on the JAX-WS JSON extension, which is a JAX-WS RI extension that lets you write one code and have it handle both SOAP and JSON requests. I just posted version 1.1 release of this extension.

The following code shows one way of deploying a JSON service:

@BindingType(JSONBindingID.JSON_BINDING)
@WebService
public class MyService {

    public Book get(@WebParam(name="id") int id) {
        Book b = new Book();
        b.id = id;
        return b;
    }

    public static final class Book {
        public int id = 1;
        public String title = "Java";
    }
}

The JSON extension comes with the equivalent of "wsimport" tool, but in a JavaScript style. Once the service is deployed, JavaScript client can load the proxy code like this:

<script src="path/to/endpoint?js"></script>

With that, it can now make service invocations as shown in the following code. The call happens asynchronously, and upon the completion the callback is invoked with the response. This is a trivial example, but since this is built on top of JAXB, it can handle any JAXB annotated beans, just like your SOAP service does.

myService.get(
  {id:5},
  function(r) {
    alert("ID="+r.id);
    alert("title="+r.title);
  }
);

The neat thing about this extension is that the system takes advantage of the schema information inside WSDL to produce JSON that looks more natural. I believe this is an unique feature not seen elsewhere in a simliar toolkit. This information is also used to generate the documentation page, so that developers writing client code can learn what format the request and the response are, without knowing anything about XML Schema. See the screenshot below to get some idea of how it looks like:

This is a part of the JAX-WS commons project, which hosts a bunch of JAX-WS related extensions, utilities, tools, etc. Anyone interested in hosting any code around the JAX-WS are welcome to join the project.

We'll be showing more about this (among other things!) in our JavaOne session TS-4948 "Unleashing the Power of JAX-WS RI: Spring, Stateful Web Services, SMTP, and More", so don't forget to plan your schedule accordingly...

Related Topics >>

Comments

how to call JSON web service from client side

hi I'm able to understand the server side JSON binding. It is more ever look like a normal jax-ws web service. Only thing i would like to know is, how to call from client side. I was bit confused regaridng 'path/to/javascript....' Can you please proivde me detail steps to call the JSON web service in detail. Thanks in advance.

it's the Endpoint publish

it's the Endpoint publish address, appended with ?js. like http://jaxws-serveraddress:portnumber/webService/theMethod?js