Search code examples
springrestspring-mvcrestletspring-ws

Implementing RESTlet in existing Java EE application


How can I implement Restlet framework with my Java EE application?

I have already tried my hands with Spring's Restful Webservice but not sure how to get started with Restlet framework.

Is it a better option than Spring MVC's RESTful implementation? What are the pros and cons of these two frameworks.


Solution

  • The strength of Restlet is that it provides complete API for REST, flexibility when using REST principles and also addresses both client and server sides.

    Another aspect you can consider is that Restlet is a complete RESTful middleware allowing connecting various and heterogeneous systems using the REST architecture. As a matter of fact, Restlet can be executed on several environments (Java, Java EE, Android, GWT, Google App Engine) and cloud platforms (EC2, GAE, Azure) with the same API in order to provide RESTful applications. It internally addresses specificities and limitations of each environment. It also allows accessing different types of REST services (like OData, S3...), integrating security of different systems (AWS, Google...) and provide support to the SDC technology of Google (accessing intranet resources in a secure way).

    Now let's enter in code. The best approach to implement a Restlet application within JavaEE is to use the servlet extension which plays the role of a front controller to this application. You can then define your entities (Application, ServerResource) as usually. You have to create the following things:

    • Restlet application (a sub class of Application):

      public class ContactApplication extends Application {
          public Restlet createInboundRoot() {
              Router router = new Router(getContext());
              router.attach("/contact/{id}",
                            SimpleContactServerResource.class);
              return router;
          }
      }
      
    • One or more server resources:

      public class SimpleContactServerResource
                            extends ServerResource {
          private ContactService contactService = (...)
      
          @Get
          public Representation getContact(Variant variant) {
              Map<String, Object> attributes
                        = getRequest().getAttributes();
              String contactId = (String) attributes.get("id");
              Contact contact = contactService.getContact(contactId);
              return new JacksonRepresentation<Contact>(contact);
          }
      
          (...)
      }
      

    Configures the Restlet servlet:

    <web-app>
        <context-param> 
            <param-name>org.restlet.application</param-name> 
            <param-value>org.restlet.gtug.gae.ContactsApplication</param-value> 
        </context-param> 
        <servlet> 
            <servlet-name>ServerServlet</servlet-name> 
            <servlet-class>org.restlet.ext.servlet.ServerServlet</servlet-class> 
        </servlet> 
        <servlet-mapping> 
            <servlet-name>ServerServlet</servlet-name> 
            <url-pattern>/*</url-pattern> 
        </servlet-mapping> 
    </web-app>
    

    Hope it helps you and gives a better view of the framework.