Skip to main content

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.
@Path( "/hello" )
public interface Greeter {
 @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 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( "",
       Greeter.class );
String result = greeter.sayHelloTo( "jiaqi" );
The server side code is even more amazing. Once interface is implemented, CXF provides library that seamlessly integrates arbitrary implementation with Servlet using Springframework. All you need to do is:

  • Making sure cxf-rt-frontend-jaxrs jar file and dependencies are in classpath. Which is simple in Maven:


  • CXF servlet and spring context listener is in web.xml
  • And tell Servlet to call my implementation in spring configuration
<beans xmlns:jaxrs="" ... >
<import resource="classpath:META-INF/cxf/cxf.xml" />
<import resource="classpath:META-INF/cxf/cxf-extension-jaxrs-binding.xml" />
<import resource="classpath:META-INF/cxf/cxf-servlet.xml" />
<jaxrs:server id="restfulService" address="/">
   <ref bean="greeter" />
<bean name="greeter"
   class="sample.GreeterImpl" ... >
CXF project has a document with super detailed examples, which talked about more flexible use cases such as arbitrary MIME type, HTTP headers, cookies and stream based API. RESTful Java with JAX-RS is a book that worth reading for JAX-RS loving people. Anyway, now it's time for me to rewrite all my services.

A caveman could do it


Popular posts from this blog

Spring, Angular and other reasons I like and hate Bazel at the same time

For several weeks I've been trying to put together an Angular application served Java Spring MVC web server in Bazel. I've seen the Java, Angular combination works well in Google, and given the popularity of Java, I want get it to work with open source. How hard can it be to run arguably the best JS framework on a server in probably the most popular server-side language with  the mono-repo of planet-scale ? The rest of this post walks through the headaches and nightmares I had to get things to work but if you are just here to look for a working example, github/jiaqi/angular-on-java is all you need. Java web application with Appengine rule Surprisingly there isn't an official way of building Java web application in Bazel, the closest thing is the Appengine rule  and Spring MVC seems to work well with it. 3 Java classes, a JSP and an appengine.xml was all I need. At this point, the server starts well but I got "No

Project Euler problem 220 - Heighway Dragon

This document goes through a Java solution for Project Euler problem 220 . If you want to achieve the pleasure of solving the unfamiliarity and you don't have a solution yet, PLEASE STOP READING UNTIL YOU FIND A SOLUTION. Problem 220 is to tell the coordinate after a given large number of steps in a Dragon Curve . The first thing came to my mind, is to DFS traverse a 50 level tree by 10^12 steps, during which it keeps track of a direction and a coordinate. Roughly estimate, this solution takes a 50 level recursion, which isn't horrible, and 10^12 switch/case calls. Written by a lazy and irresponsible Java engineer, this solution vaguely looks like: Traveler traveler = new Traveler(new Coordinate(0, 0), Direction.UP); void main() { try { traverse("Fa", 0); } catch (TerminationSignal signal) { print signal; } } void traverse(String plan, int level) { foreach(char c:plan) { switch(c) { case 'F': traveler.stepForward(); break; ca

Wreck-it Ralph is from Chicago?

Hotel Felix in Chicago   The building of Fix-it Felix Jr.