Restlet Helloworld Representative Inwards Coffee As Well As Eclipse

The Restlet is 1 of the root opened upward source frameworks to exercise in addition to deploy RESTful spider web service inward Java. After the unloosen of JAX-RS (Java API for RESTful Web Services) JSR - 317, Restlet also supports JAX-RS musical note in addition to provides a consistent agency to exercise both RESTful Server in addition to Client. HelloWorld program is the traditional agency to start amongst a novel technology scientific discipline in addition to continuing to the tradition, we'll write our root Restlet plan every bit HelloWorld. Since Restlet tin move used to exercise on both customer in addition to server side, we'll root expose a resources every bit RESTful spider web service using Restlet server in addition to thence consumer the same RESTful spider web service past times creating a RESTful client. I'll usage Maven in addition to Eclipse to exercise this RESTlet HelloWorld example, if y'all are non familiar amongst Maven, it's a construct automation tool similar ANT for Java projects but also provides dependency management i.e. y'all don't require to download Restlet JAR manually, Maven volition exercise it for you. To acquire to a greater extent than well-nigh Maven run across here.


Tools Used

We conduct hold used next tools, framework in addition to libraries to exercise in addition to execute this Restlet example
  • Java
  • Eclipse Kepler
  • Apache Maven
  • Restlet framework

Now, let's run across how the projection looks similar inward Eclipse IDE.



Eclipse Project Structure

Here is how my Eclipse projection construction await like, nosotros conduct hold 3 Java source files, RestletResource.java, RestletServer.java, and RestletClient.java. We are using an M2Eclipse plugin to usage Maven within Eclipse in addition to this projection is configured every bit Maven project. In social club to exercise the same setup on your machine, y'all require to root exercise a Maven Java project. You tin also discovery only about of my Maven Eclipse tips handy spell using Maven amongst Eclipse for Java projects.

is 1 of the root opened upward source frameworks to exercise in addition to deploy RESTful spider web service inward Jav Restlet HelloWorld Example inward Java in addition to Eclipse



Maven Dependency

You require Restlet JAR files to run our Restlet HelloWorld Example. Each Restlet framework projection to the lowest degree the restlet.jar which contains the Restlet gist module in addition to most of the usage volition also usage Jackson for JSON support. If y'all are using Maven, y'all tin only add together the next dependency inward your pom.xml and Maven volition accept help of downloading all JAR files.



One of the key matter to call upward is configuring your Maven customer to signal to a dedicated Restlet repository every bit nosotros did inward the next snippet. This snippet is truly from the Restlet website in addition to y'all tin only add together into your existing pom.xml. If y'all are using Maven projection inward Eclipse, only opened upward the pom.xml in addition to glue the next snippet there.
<repositories>     <repository>         <id>maven-restlet</id>         <name>Restlet repository</name>         <url>https://maven.restlet.com</url>     </repository> </repositories> <properties>     <restlet-version>2.3.7</restlet-version> </properties> <dependencies>     <dependency>         <groupId>org.restlet.jse</groupId>         <artifactId>org.restlet</artifactId>         <version>${restlet-version}</version>     </dependency>     <dependency>         <groupId>org.restlet.jse</groupId>         <artifactId>org.restlet.ext.jackson</artifactId>         <version>${restlet-version}</version>     </dependency> </dependencies> 

You tin run across nosotros only require restlet.jar, nosotros are using 2.0.8 version. If y'all are non using Maven y'all tin download next JAR from Maven fundamental repository.

org.restlet-2.0.8.jar
org.osgi.core-4.0.0.jar


Steps to exercise HelloWorld inward Restlet

Here are the steps y'all tin follow to exercise this HelloWorld illustration using Restlet framework:
  1. Create a Resource course of didactics past times extending ServerResource course of didactics from Restlet framework
  2. Create a Restlet Server past times creating instance of Server course of didactics from Restlet framework
  3. Connect to the URL Server is listening from a browser e.g. chrome
  4. Additionally, exercise a REST customer to eat RESTful Web service exposed past times our REST server

Now, let's sympathise the Java classes used inward this example. Btw, if y'all are non rattling familiar amongst REST in addition to Java thence I advise reading first RESTful spider web service amongst JAX-RS because Restlet also implements JAX-RS API, which is the criterion API for developing RESTful spider web services inward Java.





RestletResource.java
REST is all well-nigh the resource, anything customer is interesting is resources e.g. elevate of book, price, publisher or fifty-fifty a majority itself. RESTful Web service returns a representation of that resource. In Restlet, y'all tin exercise a RESTful resources past times extending ServerResource class. In this class, y'all tin exercise methods which are called when dissimilar HTTP method is used to access the server e.g. GET, POST or DELETE. How does Restlet know which method to telephone hollo upward on which type of HTTP request? Well, we'll usage musical note provided past times Restlet framework e.g. @Get to annotate a method which is supposed to move called when GET request access the resource, @Delete, @Post etc. These annotations are defined inward org.restlet.resource package.

import org.restlet.resource.Delete; import org.restlet.resource.Get; import org.restlet.resource.ServerResource;  public class RestletResource extends ServerResource {    @Get   public String hello() {     return "HelloWorld inward Restlet, this is GET method response";   }    @Delete   public void bye() {     System.out.println("Bye Bye, DELETE method called");   }  }


RestletServer.java
This is our RESTful Server. The main() method inward higher upward course of didactics creates an HTTP server connector, which is an instance of the Server course of didactics available inward the org.restlet package. It’s configured amongst the protocol, the listening socket port, in addition to the target Restlet that volition grip the requests—in this case, the HelloServerResource class.

After launching this Java program, y'all tin signal your spider web browser to the http://localhost:8080 URL in addition to acquire this work of text: "HelloWorld inward Restlet, this is GET method response"

import org.restlet.Server; import org.restlet.data.Protocol;  /*  * H5N1 Restlet Server, it starts a server to psyche on port 8080  * in addition to expose RestletResource every bit RESTful spider web service.  * When y'all run this program, it volition psyche for HTTP traffic  * on 8080, y'all tin access this RESTful spider web service past times connecting  * your browser to 8080  */ public class ResletServer {    public static void main(String[] args) throws Exception {     Server server = new Server(Protocol.HTTP, 8080, RestletResource.class);     server.start();   } }  Output Jun 01, 2016 12:40:43 PM org.restlet.engine.http.connector.HttpServerHelper start INFO: Starting the internal HTTP server on port 8080


RestletClient.java
Restlet non solely is a server-side framework but also gives y'all the might to write clients that eat in addition to manipulate resources exposed on the web. On running higher upward customer code inward your IDE y'all volition run across “"HelloWorld inward Restlet, this is GET method response" string on your console. That confirms that customer connected to Sever in addition to retrieve the resources using GET method. Even though this is a piffling illustration it teaches y'all lot of basic materials amongst Restlet. You tin farther expand your cognition past times reading Restlet inward Action, 1 of the best majority for developing RESTful Java spider web services using Restlet.

is 1 of the root opened upward source frameworks to exercise in addition to deploy RESTful spider web service inward Jav Restlet HelloWorld Example inward Java in addition to Eclipse


import org.restlet.resource.ClientResource;  /*  * H5N1 Restlet customer which tin eat a RESTful spider web service.  * This customer consumes REST spider web service from http://localhost:8080/.  * As oppose to browser, an RESTlet customer provides to a greater extent than pick to  * testify e.g. y'all tin telephone hollo upward whatever HTTP method e.g. GET, POST or DELETE  */ public class RestletClient {    public static void main(String[] args) throws Exception {      ClientResource customer = new ClientResource("http://localhost:8080/");     client.get().write(System.out);     // client.delete().write(System.out);;   }  }  Output Jun 01, 2016 1:27:30 PM org.restlet.engine.http.connector.HttpClientHelper start INFO: Starting the default HTTP customer HelloWorld in Restlet, this is GET method response


How to Run this Restlet Example

Since it's a customer server example, y'all require to root run the Server in addition to maintain it running. Once Server has started successfully, thence y'all tin run the client. In our client, nosotros conduct hold ii ways to test, root past times sending GET asking in addition to minute past times sending DELETE request. I conduct hold commented the DELETE asking but y'all tin root run the GET asking in addition to thence uncomment in addition to run the DELETE asking if y'all desire to testify that. Btw, if y'all are non certain well-nigh the role  of dissimilar HTTP methods inward REST world, run across here.

Here is how y'all tin start our Restlet Server, only acquire to the RestletServer.java, right-click in addition to conduct the pick "Run every bit Java application" every bit shown below:

is 1 of the root opened upward source frameworks to exercise in addition to deploy RESTful spider web service inward Jav Restlet HelloWorld Example inward Java in addition to Eclipse



Here is how our Server logs volition await similar later on starting:

is 1 of the root opened upward source frameworks to exercise in addition to deploy RESTful spider web service inward Jav Restlet HelloWorld Example inward Java in addition to Eclipse



and, hither is how our Restlet customer logs await like:

is 1 of the root opened upward source frameworks to exercise in addition to deploy RESTful spider web service inward Jav Restlet HelloWorld Example inward Java in addition to Eclipse


You tin run across from their logs that Server choke along to run but customer plan finished 1 time the asking ended.

That's all well-nigh Restlet HelloWorld illustration inward Java. In this example, y'all conduct hold learned how to exercise a Restlet server in addition to a Restlet customer which tin mail both GET in addition to DELETE request.  We conduct hold also learned how to expose an object every bit REST resources using Restlet framework.  This plan is expert plenty to start learning Restlet framework but if y'all desire to acquire it for professional person development, I strongly advise to read Restlet inward Action, it's a dandy majority amongst lots of examples, both elementary in addition to complex. H5N1 must read for whatever serious Java developer who wants to usage Restlet inward production for exposing in addition to accessing spider web services.

Further Learning
REST API Design, Development & Management
REST amongst Spring Course
answer)
  • Spring HelloWorld Example using Dependency Injection (tutorial)
  • How to exercise JDBC connectedness puddle using Spring? (tutorial)
  • 20 Hibernate Interview Questions for Java developers (article)
  • Difference betwixt Idempotent in addition to condom methods inward HTTP? (answer)
  • Top 10 REST Web Service Interview Questions (answer)
  • How to convert JSON array to String array inward Java? (tutorial)
  • How to parse large JSON response using Jackson? (tutorial)
  • 0 Response to "Restlet Helloworld Representative Inwards Coffee As Well As Eclipse"

    Post a Comment

    Iklan Atas Artikel

    Iklan Tengah Artikel 1

    Iklan Tengah Artikel 2

    Iklan Bawah Artikel