Discover how IBM SmartCloud entry to implement rest Web Service

Source: Internet
Author: User
Keywords Ibm smartcloud entry
When this rest API does not meet the needs of the user, a new rest Web Service is added. This article describes how to add a customized REST Web to IBM http://www.aliyun.com/zixun/aggregation/13696.html ">smartcloud Entry without modifying existing code" Service.

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: Full name is representational state transmits. 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: 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 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, Restlet still provided support for Jax. 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

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

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 to define rest resources and then implements the rest Web API by associating Jax 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 overview of how IBM SmartCloud Entry is implemented.

Listing 1. Associating Jax Creator 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 creator Jaxrsapplication, public testcomponent () {super ();//create Jax Runtime final Jaxrsapplication Creator = new Jaxrsapplication (GetContext (). Createchildcontext ()); Add a Jax creator to the Jax Runtime Application.add (jaxrsapplication); The JAX runtime must be added to a Component getdefaulthost (). Attach (creator); }}

Listing 1 shows how to associate Restlet and Jax Creator with Restlet extensions to Jax.

Deploying the REST Web API into a Servlet container

IBM SmartCloud Entry is built with OSGi as its core framework, and the Servlet container used is OSGi embedded Jetty. Listing 2 shows how to deploy a Restlet Component to the servlet container, where we use the Restlet extension to the servlet.

Listing 2. Deploy Restlet Component to the Servlet container

Package Com.developerworks.rest.test;import Java.util.properties;import Javax.ws.rs.ext.runtimedelegate;import Org.osgi.service.http.httpcontext;import Org.osgi.service.http.httpservice;import Org.restlet.engine.Engine; Import Org.restlet.ext.jaxrs.internal.spi.runtimedelegateimpl;import Org.restlet.ext.servlet.ServerServlet; public class Testapiwhiteboard {static {engine.getinstance (). Setuserclassloader (TestComponent.class.getClassLoader ()); Private Httpservice http; Call this function to start the REST Web Service public void startup () {Serverservlet serverservlet = new Serverservlet (); Properties InitParams = new properties (); Initparams.put ("Org.restlet.component", TestComponent.class.getName ()); HttpContext httpcontext = Http.createdefaulthttpcontext (); try {http.registerservlet ("test", Serverservlet, InitParams, HttpContext);} catch (Exception e) {e.printstacktrace ();}} public void shutdown () {http.unregister ("Test");}

Listing 2 passes the previously implemented Component as a parameter to Serverservlet and then registers the Serverservlet in the Servlet container. So far, we've been able to publish the rest Web service, but wait, our web service is empty and we haven't added rest resources yet.

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.