The World’s Leading Microsoft .NET Magazine
   
 
The .NET Addict's Blog

My Top Tags

                                                           

My RSS Feeds








I heart FeedBurner

Latest Diggs - Programming

Computers Blogs - Blog Top Sites

Site Hits

Total: 4,877,148
since: 19 Jan 2005

Building RESTful Java Web Services with JAX-RS

posted Mon 23 Feb 09

In a previous blog entry I wrote about building RESTful Web Services using Java and the JAX-WS library. As a result of JAX-WS being designed specifically as a generic, all-purpose Web Service tool that can do both WSDL and RESTful type web services, I think the syntax lacks a little bit of that simplicity that I'm striving for. That kind of simplicity I've already seen is possible using WCF's RESTful attributes and I'm constantly on the lookout for cooler, simpler, better RESTful stacks. I can't help it, I'm completely obsessed.

So this is when I found JAX-RS. You can find implementations of this in Metro, hosted by Java's GlassFish container...but you can also find a really good implementation of it in the latest snapshot of the CXF framework.

To create a RESTful Web Service using JAX-RS, all you really need to do is create a class that will be your service and then your container (in my case Tomcat) and CXF do the rest. Here's a snippet of a sample class from a CRM service that has been decorated with JAX-RS/RESTful attributes:

// misc imports
@Path("/")
@Produces("application/xml")
public class CustomerService implements ApplicationContextAware {

    // private member variables go here.

    @GET
    @Path("/customers")
    public CustomerList getCustomers() {
        // code to build customer list goes here
    }

    @GET
    @Path("/customers/{customerId}")
    public Customer getCustomer(@PathParam("customerId") String customerId) {
        // code to fetch individual customer goes here
    }
}

As you can see, there's a /customers URL and a /customers/### URL that allow you to retrieve either a full list of customers or a payload for a single customer. It's pretty self-explanatory from just looking at the code (which I like). One thing you'll notice is that the service class is application context aware, which means I'm going to be creating an instance of my JAX-RS server using spring. Below is a little snippet of the spring.xml file that instantiates (or injects, depending on who you ask) the JAX-RS server:

<bean id="crmService" class="com.dotnetaddict.samples.CustomerService"
init-method="init"/>

<jaxrs:server id="customerServerJaxRs" address="/crm">
    <jaxrs:serviceBeans>
        <ref bean="crmService"/>
    </jaxrs:serviceBeans>
</jaxrs:server>

In short, this is yet another way that you can build RESTful Web Services using Java. I prefer the JAX-RS way of doing things over the JAX-WS way of doing things mostly because the syntax is specifically designed to be RESTful and RESTful isn't just some kind of add-on that you can tack on to an otherwise WSDL-type Web Service.

However, my quest for the simplest and easiest RESTful syntax that is also applicable in the enterprise isn't over yet :)

tags:                    

links: digg this    del.icio.us    technorati    reddit

AddThis Social Bookmark Button




1. Kevin Hoffman left...

So to clarify, the above code combined with the above spring.xml file creates a URL at (server)/crm/customers and another URL at (server)/crm/customers/(id)


Tag Related Posts

Building RESTful Java Web Services with JAX-RS

Mon 23 Feb 09 8:42 P GMT-05
tags:                    

Upgrading your Leopard install to Java SE 6 64-Bit

Mon 12 Jan 09 1:38 P GMT-05
tags:            

Know thine Enemy: RESTful Web Services in ... Java!

Sun 11 Jan 09 2:08 P GMT-05
tags:                    

Enterprise Web Services Manifesto - Wire Formats

Tue 02 Sep 08 1:43 P GMT-05
tags:                

MobileMe vs. Live Mesh - Round 1

Wed 11 Jun 08 12:20 A GMT-05

CLINQ v1 Demo - Network Message Filtering

Wed 09 Jan 08 7:47 P GMT-05
tags:        

LINQ to REST - A much better name for Astoria

Tue 11 Dec 07 1:23 P GMT-05
tags:        

Orcas' Hidden Gem - The managed PNRP stack

Fri 11 May 07 6:45 P GMT-05
tags:        

On MUDs

Thu 08 Mar 07 5:00 A GMT-05
tags:                    

What I think is a bug in WCF POX messaging

Thu 04 Jan 07 4:58 P GMT-05
tags:      

Ulysses Agenda Makes Redmond Developer News

Wed 29 Nov 06 7:10 P GMT-05
tags:                

Ulysses Agenda - Network Engine Test 1

Mon 09 Oct 06 2:26 A GMT-05
tags:              

Ulysses Agenda : First Cut Networking Design

Thu 14 Sep 06 12:46 A GMT-05
tags:                  

First Impressions of Windows Vista RC1

Thu 07 Sep 06 1:30 P GMT-05
tags:                      

.NET Framework 3.0 June CTP is out!

Fri 23 Jun 06 6:23 P GMT-05
tags:                

Tech-Ed 2006 - Session Reviews

Tue 13 Jun 06 6:22 P GMT-05
tags: