JAX-WS, the new web service implementation replacing JAX-RPC; provides very interesting features.\\
It is amazing how a web service can be generated and published.\\
Let's see an example of a web service that allows pulling news headlines from EGJUG.\\
We will start by creating an XML schema that is the messaging contract between the client and the server:
{{{
}}}
Very simple, a request will specify the portal name it needs to pull news from, and the response will return those headlines if any.
Now let's create a WSDL for our web service:
{{{
}}}
There is only one operation here: 'getHeadLines'.
The operation will hold the requested news portals as an input.
Starting from a WSDL, JAX-WS provides some handy tools to generate all web service artifacts, a batch file named 'wsimport.bat' ('wsimport.sh' for Unix) can be found under the 'bin' directory of the distribution, a simple command line will do the job:
{{{
wsimport -keep -verbose -s src -d classes -wsdllocation http://127.0.0.1:7777/services/simple?wsdl xml/simple-service.wsdl
}}}
This will generate all needed artifacts. XML binding is generated using JAXB 2.0. No more different binding libraries like JAX-RPC.
Now let's implement our 'NewsHeadLines' service:
{{{
@javax.jws.WebService(endpointInterface = "org.egjug.services.simple.NewsHeadLines")
public class SimpleService{
@WebMethod
@WebResult(name = "response", targetNamespace = "http://www.egjug.org/xml/ns/news", partName = "response")
public Response getHeadLines(
@WebParam(name = "request", targetNamespace = "http://www.egjug.org/xml/ns/news", partName = "request") Request request){
Response response = new Response();
List responsePortals = response.getPortal();
List requestedPortals = request.getPortal();
for(Portal rqp : requestedPortals){
Portal rsp = new Portal();
rsp.setName(rqp.getName());
addHeadLines(rsp);
responsePortals.add(rsp);
}
return response;
}
private void addHeadLines(Portal rsp){
List headlines = rsp.getHeadLine();
switch(rsp.getName()){
case TSS:
headlines.add("BemBem Software just released a new AJAX product, this release can make coffee");
headlines.add("Hani Sulieman interview, watch this big mouth today");
break;
case IBM:
headlines.add("IBM releases another buggy Rational product 'Rational XeSdf0I', download and crash today");
headlines.add("IBM releases a DB2 version for free, go get Oracle's free one");
break;
}
}
}
}}}
Of course news wouldn't be statically embedded into the code; this is just for testing the service.
As mentioned earlier, one powerful feature of this API is the simplicity of web services publishing, here is an example:
{{{
public class SimplePublisher{
public static Source load(String uri){
Source source = null;
try{
source = new SAXSource(new InputSource(new FileInputStream(uri)));
source.setSystemId(new File(uri).toURL().toString());
}
catch(FileNotFoundException e){
e.printStackTrace();
}
catch(MalformedURLException e){
e.printStackTrace();
}
return source;
}
public static void publish(){
Provider provider = Provider.provider();
List metadata = new ArrayList();
metadata.add(load("xml/egjug-news.xsd"));
metadata.add(load("xml/simple-service.wsdl"));
SimpleService simple = new SimpleService();
Endpoint endpoint = provider.createEndpoint(SOAPBinding.SOAP11HTTP_BINDING, simple);
endpoint.setMetadata(metadata);
endpoint.publish("http://127.0.0.1:7777/services/simple");
}
public static void main(String...args){
publish();
}
}
}}}
This is all what we need to do to publish the service, no application server, no servlet configuration, no deployment, just a simple API, but the API also supports running inside a container as well.
To test our service, let's send a SOAP message. You can use any client or code your own:
{{{
}}}
Here is the reply for this message:
{{{
BemBem Software just released a new AJAX product, this release can make coffeeHani Sulieman interview, watch this big mouth today
}}}
|>
So simple, start writing your web service today!!
Submitted by Ahmed Hashim on Fri, 2006-02-10 15:51.
This article is the tip of the week on [Java.net Web service|http://community.java.net/java-ws-xml/]
Keep on the Good work hplusplus
-------------%%%
__Ahmed Hashim__%%%
This article is the tip of