Java Reference
In-Depth Information
Using SAAJ to Access a RESTful Service
Problem
You want to use the SAAJ library to access a RESTful web service, or at least have an easy
way to talk POX (Plain Old XML) over HTTP.
Solution
No problem. Get a RequestContext object from your Dispatch . Then invoke requestCon-
text.put(MessageContext.HTTP_REQUEST_METHOD, "GET") .
Discussion
You used these classes throughout the JAX-WS chapters, so I won't belabor the point now.
But Example 8-23 shows a complete example of the sort of thing you want to do.
Example8-23.UsingaSAAJclienttoinvokeaRESTfulservicethattalksoverHTTPand
returns plain XML
package com.soacookbook.rest.saaj;
import java.net.URI;
import java.util.Map;
import javax.xml.namespace.QName;
import javax.xml.transform.Source;
import javax.xml.transform.Transformer;
import javax.xml.transform.TransformerFactory;
import javax.xml.transform.dom.DOMResult;
import javax.xml.ws.Dispatch;
import javax.xml.ws.Service;
import javax.xml.ws.handler.MessageContext;
import javax.xml.ws.http.HTTPBinding;
import javax.xml.xpath.XPath;
import javax.xml.xpath.XPathConstants;
import javax.xml.xpath.XPathExpressionException;
import javax.xml.xpath.XPathFactory;
import org.w3c.dom.NodeList;
/**
* Using SAAJ to call a REST service that produces raw XML
Search WWH ::




Custom Search