Java Reference
In-Depth Information
A RESTful Service with JAX-WS
Problem
You want to build a RESTful web service using a lightweight framework such as JAX-WS,
giving your service a little more order than a plain servlet does.
Solution
Implement the
Provider<Source>
interface on your POJO with the annotation
@WebSer-
viceProvider
and a binding type of
HTTPBinding
.
Discussion
As you may recall, the
@WebServiceProvider
annotation is defined in the JAX-WS specific-
ation. This annotation allows you to work at the XML message level. If you're doing straight
JAX-WS, the alternatives to parameterizing on
Source
are either
DataSource
or
SOAPMes-
sage
, but you can't use those here. Because the
Provider
gives you access to the XML in the
message, you don't have to use JAXB anywhere in this example.
Recall that
Provider
defines only one method, which has the following signature:
public T invoke(T request)
Parameterize on
Source
here and use the required annotation. The final thing you need to do
is specify the
BindingType
as HTTP. So this is the basic shell of a RESTful service done in
this fashion:
import javax.xml.transform.Source;
import javax.xml.ws.Provider;
import javax.xml.ws.WebServiceProvider;
import javax.xml.ws.http.HTTPBinding;
@WebServiceProvider
@BindingType(HTTPBinding.HTTP_BINDING)
public class JaxWsRestfulService implements Provider<Source> {
public Source invoke(Source request) {
//do work...
}
}
The binding type specification is crucial here, as it tells the container to talk straight XML
over HTTP, and not to use SOAP messages (which is what you would get by default).