Skip to main content

Posts

Showing posts from January, 2010

RESTful service with JAX-RS is so easy, a caveman could do it

A recent project requires me to build a RESTful service that supports supports stream input, output and JAXB binding. After a quick research, I was amazed when I found how easy it is to build RESTful service using JAX-WS(JSR-311) and Apache CXF . Cut the chase, let the code talk. import javax.xml.ws.rs.*; @Path( "/hello" ) public interface Greeter { @GET @Path( "/{user}") @Produces( "text/plain" ) public String sayHelloTo( @PathParam( "user" )String userName ); } It seems the annotations in the interface above provides everything client needs to know, and that's exactly right. Following CXF class returns an implementation of Greeter that knows to send HTTP request to http://helloworld.com/hello/jiaqi and return the HTML response as a String. There's zero coding work involved to create fully functional client. import org.apache.cxf.jaxrs.client.JAXRSClientFactory; ... Greeter greeter = JAXRSClientFactory.create( "http://hel