Getting the Apache CXF Jax-RS Basic sample running

I did it in January – just came back to it today and could not remember what I did. Where is my memory? So for my benefit, and perhaps the benefit of others, I’m writing it down. There are hints in the README that ships with the sample, but it was far from step by step.Of course, there are a number of different ways you can get this running – this is just one way.My environment:

  • FUSE Services Framework (CXF) version 2.1.3.2
  • Eclipse 3.3.2

And here’s what you do.

  1. Start up Eclipse with a new or existing workspace and use the Java perspective.
  2. Create a new project by selecting File->New->Other… and then Java->Java Project from Exiting Ant Buildfile. Enter the name of the project, something like “Basic Jax-RS Sample”. Browse to {FUSE Install dir}/samples/jax_rs/basic/build.xml. Check the box that reads “Link to the buildfile in the file system” and click Finish. Note that this checkbox does NOT make a copy of the sample directory, nor does it create a separate build.xml, rather uses the one that is in the sample directory. This build file references another build file that is relative to the build.xml file so in order to use the build file without changes it must be invoked from that directory.

This is all you need to do to set the project up. Now you can run it. We are going to do so all inside of eclipse. First we’ll run the server part of the sample and use a browser to invoke the services – just reads. Then we’ll run both the server and a simple client application from within eclipse. Read on.

Running the Server and GETting resources

  1. Expand the Basic Jax-RS Sample project
  2. Right click on the build.xml file and select “Run As->Ant Build…” – be sure to to select the “Ant Build…” with the dot-dot-dot so you can select what you want to build and run.
  3. Now you can fetch some resources – note that a few are created for you when you run the server. Invoke a URL either http://localhost:9000/customerservice/customers/123 or http://localhost:9000/customerservice/orders/223/products/323. Notice in the code that customers, via the path “customers/{id}” and products, via the path “orders/{oid}/products/{pid}” are the only two resources that support GET.

Also notice that the server only stays up and running for 5 minutes by default – if you wish to lengthen this you can change the line of code in the Server.java file that reads:Thread.sleep(5 * 60 * 1000);And that’s it. You are running the application. If you want to test out some write operations…

Running the Server and a Client Application

Both the server and the client can be run from within eclipse.

  1. If your server is not running follow the steps outlined in the section above – basically do a Run As->And Build… and select the server.
  2. Now run the client by right-click on the build.xml, Run As->Ant Build… and this time select “client” (deselect “server”)
  3. You can see the results of the execution in the console pane. Customer number 123 was updated and a new customer was created and assigned an id of 124 by the server. In fact you can now invoke the URL http://localhost:9000/customerservice/customers/124 to see that new customer resource.

That’s it, the basics of getting the application up and running.

Share Your Thoughts