![]() | |
| |
JAX-WS supports a "pluggable encoding" - meaning it can use formats other than textual XML. This extension takes advantage of this and allows JAX-WS services to be exposed via JSON.
JSON support is implemented as a custom binding. So just like there are SOAP/HTTP binding or Plain Old XML binding, you can specify JSON binding to expose a service as JSON service. The following code shows one way of exposing a service over JSON:
@BindingType(JSONBindingID.JSON_BINDING)
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";
}
}
You just need jaxws-json.jar in your WEB-INF/lib for this to work.
Client-side Programming
Once the server exposes a JSON service, you'll write client-side JavaScript to access this service. First, you'll include the proxy script.
<script src="path/to/endpoint?js"></script>
This creates a variable 'myService' with which you can make service invocations. All the parameters are passed as a single JavaScript object, where the property name is the parameter name. The call happens asynchronously, and when it's done your callback will be invoked with the return value as a parameter:
myService.get(
{id:5},
function(r) {
alert("ID="+r.id);
alert("title="+r.title);
}
);
If you'd like to use a different variable name, specify that as the var query parameter, as shown below:
<script src="path/to/endpoint?js&var=svc"></script>
For client-side development, JSON endpoints serve HTML documentations on the fly, including all the available operations and "type descriptions" for all parameters and return values.
|
|
|
|
Hosting provided by PerfoHost: KVM VPS. Unix VPS. Windows VPS. VPN. Domains. Dedicated servers. Colocation.