Accessing the body of a post RESTful request
I can completely read a post request (headers + raw body) using
com.sun.net.httpserver.HttpServer
with an HttpHandler for it.
But I can't do the same using a javax.xml.ws.WebServiceProvider
I can read the headers, but I need the raw body data in the开发者_StackOverflow社区 request be it plain text, xml, or data-bytes. An InputStreamReader would be OK.
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)
@WebServiceProvider
@BindingType(value = HTTPBinding.HTTP_BINDING)
public class MyRestHandler
implements Provider<Source> {
@Resource
protected WebServiceContext wsContext; // injected
public Source invoke(Source source) {
/* source happens to be null except for text/xml BODY type
* in the incoming request.
* In this case, source is a SAXSource*/
}
}
Addendum (after Peter Knego observation)
In Java Web Services pages 126-138, the author presents a RESTful webservice example where JAX-WS is used:
@ServiceMode(value = javax.xml.ws.Service.Mode.MESSAGE)
@WebServiceProvider
@BindingType(value = HTTPBinding.HTTP_BINDING)
public class RestfulTeams
implements Provider<Source> { ... }
Even, in page it 136-137 it extends it to support the POST method, accessing
the body of the request. Clearly, it works with the JAX-WS library classes because it uses xml in the body request. Now I understand your surprise, and why source
is always null but for text/xml bodies. Really, the easiest way was to use the JAX-RS library.
Use a JAX-RS implementation, Jersey for instance. Book: RESTful Java
精彩评论