Environmental preparedness
A running SmartCloud Entry system
Eclipse 3.6 or later
Discover how IBM smartcloud Entry to implement REST Web Service
Before you start
Instead of building its own REST Web Service from Brick, IBM SmartCloud Entry a number of tools. Before you begin, you will need to have some knowledge of the following nouns.
REST: The full name is representational state transfer. It is a WEB service style that is lightweight, easy to build, stateless, and uses the HTTP protocol, as compared to traditional Web services (based on SOAP).
Jax-rs: The full name is the Java TM API for RESTful Web Services, a Java programming language application interface. It provides support for creating restful Web Service. With annotations introduced from Java SE 5 (Annotation), Jax-rs can simplify the process of deploying Web Service.
Restlet: Born in 2005, is an open source project for Java language developers. Restlet is designed to provide developers with a way to implement REST Web Service in various scenarios in a simple way. Although Restlet had existed long before the birth of Jax-rs, Restlet still provided support for Jax-rs. The latest stable version of Restlet is 2.1.1, and everything in this article is based on Restlet 2.1.
OSGi: The full name, Open Services Gateway Initiative, has been viewed as a full-fledged modular system framework that typically includes desktop applications, WEB applications, mobile applications, and middleware. It provides an underlying infrastructure that can be used to develop modular, dynamic service-oriented applications.
Restlet realize Jax-rs
In Restlet 2.1.1, the Restlet can be divided into two parts:
REST Application Development Core: Contains Restlet APIs and Restlet Engine
Extension: Contains Restlet extensions to support various technologies, such as Servlet and Jax-rs
If you have a little understanding of Restlet, you know that the rest Web API can be implemented by only the Core of Restlet. IBM SmartCloud Entry uses Jax-rs to define rest resources and then implements the rest Web API by associating Jax-rs Restlet and rest resources with Restlet extensions to Engine. For the pros and cons of these two ways, do not repeat here, you can find instructions on the Internet. The following is a brief introduction to how IBM SmartCloud Entry is implemented.
Listing 1. Associating Jax-rs application with Restlet
Package com.developerworks.rest.test;
Import org.restlet.Component;
Import org.restlet.ext.jaxrs.JaxRsApplication;
Import javax.ws.rs.core.Application;
public class Testcomponent extends Component {public
static application jaxrsapplication;
Public testcomponent () {
super ();
Create Jax-rs Runtime
final jaxrsapplication application =
new Jaxrsapplication (GetContext (). Createchildcontext ());
Add a Jax-rs application to the Jax-rs runtime
application.add (jaxrsapplication);
The JAX-RS runtime must be added to a Component
getdefaulthost (). Attach (application);
}
Listing 1 shows how to associate Restlet and JAX-RS application with Restlet extensions to Jax-rs.
See more highlights of this column: http://www.bianceng.cnhttp://www.bianceng.cn/Programming/extra/