 |
JAX-WS RI extension for JSON
Posted by kohsuke on April 27, 2007 at 03:42 PM | Comments (9)
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...
Bookmark blog post: del.icio.us Digg DZone Furl Reddit
Comments
Comments are listed in date ascending order (oldest first)
-
This looks really cool, since you can provide both SOAP and JSON.
Can you elaborate what's happening behind the curtains, when a JSON request to your WS is issued?
Posted by: dlinsin on May 01, 2007 at 03:03 AM
-
It would be nice if the dependencies were listed. I got the jaxws-json-1.2.jar and then got a runtime error about missing json classes... so had to troll and build the json jar and then got errors about missing org/codehaus/jettison classes.. which found and downloaded..
Finally it have a nullpointer
java.lang.NullPointerException
at org.jvnet.jax_ws_commons.json.schema.JsonOperation.build
At which point I gave up.
/s
Posted by: sameer_t on September 05, 2007 at 10:04 AM
-
Yes, for non-maven users finding all the required dependencies is PITA. We are thinking about posting a distribution that includes everything. So stay tuned.
As for NPE, please file an issue.
Posted by: kohsuke on September 05, 2007 at 11:24 AM
-
Any news on this bug? I had to jump through some hoops to get this working.
I am getting the following error on tomcat 6
java.lang.NullPointerException
at org.jvnet.jax_ws_commons.json.schema.JsonOperation.(JsonOperation.java:36)
at org.jvnet.jax_ws_commons.json.SchemaInfo.buildJsonSchema(SchemaInfo.java:268)
at org.jvnet.jax_ws_commons.json.SchemaInfo.(SchemaInfo.java:141)
at org.jvnet.jax_ws_commons.json.JSONCodec.setEndpoint(JSONCodec.java:63)
at com.sun.xml.ws.server.WSEndpointImpl.(WSEndpointImpl.java:158)
at com.sun.xml.ws.server.EndpointFactory.createEndpoint(EndpointFactory.java:217)
at com.sun.xml.ws.api.server.WSEndpoint.create(WSEndpoint.java:467)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parseAdapters(DeploymentDescriptorParser.java:253)
at com.sun.xml.ws.transport.http.DeploymentDescriptorParser.parse(DeploymentDescriptorParser.java:147)
at com.sun.xml.ws.transport.http.servlet.WSServletContextListener.contextInitialized(WSServletContextListener.java:108)
at org.apache.catalina.core.StandardContext.listenerStart(StandardContext.java:3827)
at org.apache.catalina.core.StandardContext.start(StandardContext.java:4334)
at org.apache.catalina.core.ContainerBase.addChildInternal(ContainerBase.java:791)
at org.apache.catalina.core.ContainerBase.addChild(ContainerBase.java:771)
at org.apache.catalina.core.StandardHost.addChild(StandardHost.java:525)
at org.apache.catalina.startup.HostConfig.deployDescriptor(HostConfig.java:626)
at org.apache.catalina.startup.HostConfig.deployApps(HostConfig.java:511)
at org.apache.catalina.startup.HostConfig.check(HostConfig.java:1220)
at sun.reflect.NativeMethodAccessorImpl.invoke0(Native Method)
at sun.reflect.NativeMethodAccessorImpl.invoke(NativeMethodAccessorImpl.java:39)
at sun.reflect.DelegatingMethodAccessorImpl.invoke(DelegatingMethodAccessorImpl.java:25)
at java.lang.reflect.Method.invoke(Method.java:597)
at org.apache.tomcat.util.modeler.BaseModelMBean.invoke(BaseModelMBean.java:297)
at com.sun.jmx.interceptor.DefaultMBeanServerInterceptor.invoke(DefaultMBeanServerInterceptor.java:836)
at com.sun.jmx.mbeanserver.JmxMBeanServer.invoke(JmxMBeanServer.java:761)
at org.apache.catalina.manager.ManagerServlet.check(ManagerServlet.java:1458)
at org.apache.catalina.manager.ManagerServlet.deploy(ManagerServlet.java:820)
at org.apache.catalina.manager.ManagerServlet.doGet(ManagerServlet.java:348)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:690)
at javax.servlet.http.HttpServlet.service(HttpServlet.java:803)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:290)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.netbeans.modules.web.monitor.server.MonitorFilter.doFilter(MonitorFilter.java:174)
at org.apache.catalina.core.ApplicationFilterChain.internalDoFilter(ApplicationFilterChain.java:235)
at org.apache.catalina.core.ApplicationFilterChain.doFilter(ApplicationFilterChain.java:206)
at org.apache.catalina.core.StandardWrapperValve.invoke(StandardWrapperValve.java:230)
at org.apache.catalina.core.StandardContextValve.invoke(StandardContextValve.java:175)
at org.apache.catalina.authenticator.AuthenticatorBase.invoke(AuthenticatorBase.java:525)
at org.apache.catalina.core.StandardHostValve.invoke(StandardHostValve.java:128)
at org.apache.catalina.valves.ErrorReportValve.invoke(ErrorReportValve.java:104)
at org.apache.catalina.core.StandardEngineValve.invoke(StandardEngineValve.java:109)
at org.apache.catalina.connector.CoyoteAdapter.service(CoyoteAdapter.java:261)
at org.apache.coyote.http11.Http11Processor.process(Http11Processor.java:844)
at org.apache.coyote.http11.Http11Protocol$Http11ConnectionHandler.process(Http11Protocol.java:581)
at org.apache.tomcat.util.net.JIoEndpoint$Worker.run(JIoEndpoint.java:447)
at java.lang.Thread.run(Thread.java:619)
the problem seems to be that the variable "bo" passed to the constructor public JsonOperation(WSDLBoundOperation bo, XSSchemaSet schemas, JsonTypeBuilder builder, Style style) contains no output stream (bo.getOperation().getOutput() returns null) - this makes sense since this method is called when the server starts - not due to a httprequest.
Also, on Glassfish V2, if I put jaxws-json.jar in WEB-INF/lib the server does not pick up the BindingIDFactory (META-INF.services is not read). A quick fix would be to put this in a location that is loaded when the rest of the files for the server are, though this presents some problems of its own.
Where do I file these issues? In any case, I will be looking through the code for attempting some fixes, as this is something I really want to see working.
Posted by: tofarr on October 13, 2007 at 11:06 AM
-
Akk!! sorry for that - should have formatter with pre
Posted by: tofarr on October 13, 2007 at 11:13 AM
-
Hi,
I'd like to ask you whether is possible to run JAX-WS RI ext. with JSON without Spring ? Or how to configure it ? I am using Tomcat6, everything works fine
PS: I was very hard to make it work without any documentation :-)
thx
Posted by: kapcus on October 30, 2007 at 02:55 AM
-
The code above is demonstated without swing, and I was able to get it working with some adaptions, though I didn't run many tests on it for bugs - it seemed almost unfinished. The author has not replied in a while, so I am asuming the project is dead in the water for now. My advice is to try axis 2.
Posted by: tofarr on October 31, 2007 at 03:10 PM
-
As for NPE or any other issues, please file an issue in the issue tracker.
And yes, this works without using Spring — top-page example shows you how to configure this w/o Spring.
Posted by: super_kohsuke on November 01, 2007 at 04:19 PM
-
Does this work on Sun Java System Web Server? I'm trying it and I keep getting the following exception:
WSSERVLET11: failed to parse runtime descriptor: java.lang.IllegalArgumentException: Wrong binding id https://jax-ws-commons.dev.java.net/json/ in @BindingType
The jaxws-json-1.2.jar file is already in the WEB-INF/lib folder.
Posted by: ronak2121 on March 24, 2008 at 07:46 AM
|