![]() |
|||||||||||||||||||||||||
|
|
|||||||||||||||||||||||||
Conference Tools
|
RESTful Web Services Made Easyby Ed Ort
Representational State Transfer (REST) has become a popular approach to developing web services. Many developers see the REST architectural style as much simpler than SOAP, the prevalent web services approach before REST. A lot of that simplicity is the result of some basic concepts that underlie REST. For example, everything in REST is seen as a resource that is identified by a Universal Resource Identifier (URI). And REST presents a standard set of methods with which you communicate with a resource through a uniform interface. The Java API for RESTful Web Services (JAX-RS) is a technology that makes it easy for Java developers to build RESTful web services -- web services that adhere to the REST architectural style. The latest release of JAX-RS, JAX-RS 1.1, will be included in Java EE 6, the next version of Java Platform, Enterprise Edition. In the 2009 JavaOne Conference technical session titled, Developing RESTful Web Services With the Java API for RESTful Web Services (JAX-RS), Sun engineers Marc Hadley and Paul Sandoz, the specification leads for JAX-RS, presented the basics of the technology and demonstrated how easy the technology is to use. A JAX-RS Tutorial
Hadley handled the tutorial part of the session, which started with a discussion of basic concepts. One of the most important underpinnings of JAX-RS is that it's annotation based. So typically, a lot of the work in building a RESTful web service with JAX-RS is simply creating and annotating Plain Old Java Objects (POJOs) -- in other words, simple Java classes. Each of these classes represents a resource in JAX-RS. Hadley noted, however, that resources do not necessarily have to be classes. "If you're in an EE container, you can use EJBs [Enterprise JavaBeans]." For example, the code for a resource in JAX-RS might look something like this:
@Path ("properties");
public class SystemProperties {
@GET
LIST<SystemProperty> getProperties (...) {...}
@Path("{name}")
SystemProperty getProperty (...) {...}
}
The
Requests and responses in JAX-RS contain resource representations in formats
identified by media types. These media types include XML, JSON, HTML, XHTML.
For example, a
@GET
@Produces("application/properties+xml")
Property getXML(@PathParam("name") String name)
{...}
As you might guess, the
One other thing to keep in mind is that responses contain URIs that link to
further resources. For example, a response that contains XML might look
something like the following:
HTTP/1.1 201 CREATED
Date: Wed, 031 Jun 2009 16:41:58 GMT
Server: Apache/1.3.6
Location: http://example.com/properties/foo
Content-Type: application/jorder+xml
Content Length: 184
<property self="http://example.com/properties/foo">
<parent ref="http://example.com/properties/bar">
<name>Foo</name>
<value>1</value>
</order>
Deployment Flexibility
Hadley then focused on deployment options provided by JAX-RS. The JAX-RS specification identifies two ways for deploying JAX-RS resources. A JAX-RS application can be deployed on a Java Platform, Standard Edition (Java SE) endpoint. In this case, the deployer needs to supply some configuration information such as a list of resource classes and providers.
In Java EE, a JAX-RS application can be packaged in a WAR file just like a
servlet. In addition, a resource class can be deployed as an EJB session bean
or a singleton bean. When a Java EE container becomes JAX-aware, the Demonstration
Perhaps most impressive in this session was a set of JAX-RS demonstrations that Sandoz showed using NetBeans 6.7 (currently available as an RC2 download) and GlassFish v3 Preview. GlassFish v3 Preview is an open-source application server that implements Java EE 6. Sandoz showed how easy it is to create and change JAX-RS resources, and then deploy and run applications that use the resulting RESTful web services. Sandoz told the audience that what they should get out of these demonstrations is the idea that "it's easy to play with and test your assumptions in JAX-RS." He indeed proved the point. Status
Hadley ended the session with a JAX-RS roadmap. The JAX-RS 1.0 specification was finalized in October 2008. Work on a maintenance release, JAX-RS 1.1, is completed. Work has also begun on a new functional release, JAX-RS 2.0, which will include significant enhancements such as the inclusion of a client API. For More Information
You can get more information about JAX-RS and its reference implementation, Jersey, at the following locations:
» JSR 311: JAX-RS: The Java API for RESTful Web Services Other Resources
» Java EE 6 Technologies Do you have comments about this article? We welcome your participation in our community. Please keep your comments civil and on point. You may optionally provide your email address to be notified of replies - your information is not used for any other purpose. By submitting a comment, you agree to these Terms of Use. |
||||||||||||||||||||||||
ContactUs | About Sun | Privacy | Terms of Use | Trademarks Conference content is subject to change. Copyright 1996 - 2009 Sun Microsystems, Inc. |
![]() |
|