Search |
||
RESTful Web Services with JAX-WSPosted by mhadley on January 10, 2006 at 1:58 PM PST
The JAX-WS implementation that is included in the, recently released, JWSDP 2.0 preview now supports the XML/HTTP binding. This new functionality allows JAX-WS applications to implement and use RESTful Web services. Here is a worked example that demonstrates how to use JAX-WS to query the Yahoo News Search service. The first step is to create a JAX-WS port for the Yahoo News Search service
URI nsURI = new URI("urn:yahoo:yn");
QName serviceName = new QName("yahoo",nsURI.toString());
QName portName = new QName("yahoo_port",nsURI.toString());
Service s = Service.create(serviceName);
URI address = new URI("http", null, "api.search.yahoo.com", 80,
"/NewsSearchService/V1/newsSearch",
"appid=jaxws_restful_sample&type=all&results=10&sort=date&language=en&query=java",
null);
s.addPort(portName, HTTPBinding.HTTP_BINDING, address.toString());
Note that the URI constructed above contains a set of input parameters to the search service. The Once the port is created we can use the
Dispatch<Source> d = s.createDispatch(portName, Source.class, Service.Mode.PAYLOAD);
Map<String, Object> requestContext = d.getRequestContext();
requestContext.put(MessageContext.HTTP_REQUEST_METHOD, new String("GET"));
Now that we have a Source result = d.invoke(null); DOMResult domResult = new DOMResult(); Transformer trans = TransformerFactory.newInstance().newTransformer(); trans.transform(result, domResult); Now we have the results as a DOM tree we can apply XPath queries to extract the data we are interested in. Here we first use an XPath query to obtain the number of search results returned and then use that knowledge to interate through the results using additional queries.
XPathFactory xpf = XPathFactory.newInstance();
XPath xp = xpf.newXPath();
xp.setNamespaceContext(new NSResolver("yn", nsURI.toString()));
NodeList resultList = (NodeList)xp.evaluate("/yn:ResultSet/yn:Result", domResult.getNode(),
XPathConstants.NODESET);
int len = resultList.getLength();
for (int i=1;i<=len;i++) {
String title = xp.evaluate("/yn:ResultSet/yn:Result["+i+"]/yn:Title", domResult.getNode());
String click = xp.evaluate("/yn:ResultSet/yn:Result["+i+"]/yn:ClickUrl", domResult.getNode());
System.out.printf("[%d] %s (%s)\n",i,title,click);
}
The I mentioned above that there's no need to create a new JAX-WS port when the input data changes, the following lines submit a new query using the existing objects we've already created.
requestContext.put(MessageContext.QUERY_STRING,
"appid=jaxws_restful_sample&type=all&results=10&sort=date&language=en&query=solaris");
result = d.invoke(null);
That's it for now, in future blogs I'll demonstrate how to use JAXB instead of the XPath APIs to get strongly typed access to the results and how WADL can be used to generate JAX-WS based stubs for RESTful services. »
Related Topics >>
Web Services and XML Comments
Comments are listed in date ascending order (oldest first)
|
||
|
|