RESTful WS using Jersey: "Hello world!!!"

JAX-RS is a java specification which define APIs to implement RESTful web services. Jersey is a reference implementation of JAX-RS. It provides libraries to develop RESTful web services based on the JAX-RS specification. It uses a servlet to receive and dispatch client requests to right execution classes. In this tutorial, we will develop a simple service which returns string “Hello world!” to client using Jersey.

Prerequisites

All needed tools are also provided in https://www-inf.telecom-sudparis.eu/~gaaloulw/Downloads.

Step 1: setting up Tomcat on Eclipse

Step 2: Create your project

  1. Open Eclipse, File→New→Dynamic Web Project.
  2. Name the project: rest. Select Target runtime as your Apache Tomcat. Click Finish.
  3. Download and extract the Jersey package.
  4. Copy all jar files from lib, ext and api folders to the WebContent→WEB-INF→lib folder
  5. Create new package in the src folder. Name it: my.first.rest.
  6. Create new Java class inside this package. Name it: Hello.
  7. Edit it as following:

    package my.first.rest;
     
    import javax.ws.rs.GET;
    import javax.ws.rs.Path;
    import javax.ws.rs.Produces;
    import javax.ws.rs.core.MediaType;
     
    @Path("hello") //set your service url path to <base_url>/hello
                   // the <base_url> is based on your application name, the servlet and the URL pattern from the web.xml configuration file
    public class Hello {
     
      // This method is called if TEXT_PLAIN is requested
      @GET
      @Produces(MediaType.TEXT_PLAIN) // defines which MIME type is delivered by a method annotated with @GET
      public String sayHelloInPlainText() {		  
        return "Hello world!";
      }
     
      // This method is called if HTML is requested
      @GET
      @Produces(MediaType.TEXT_HTML)
      public String sayHelloInHtml() {
        return "<html> " + "<title>" + "Hello world!" + "</title>"
            + "<body><h1>" + "Hello world!" + "</body></h1>" + "</html> ";
      }
    }

Step 3: Define the servlet container

  1. Create a file: web.xml in the WebContent→WEB-INF folder.
  2. Insert the following code into the web.xml file:

    <?xml version="1.0" encoding="UTF-8"?>
    <web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_4_0.xsd" id="WebApp_ID" version="4.0">
      <display-name>rest</display-name>
      <welcome-file-list>
        <welcome-file>index.html</welcome-file>
        <welcome-file>index.htm</welcome-file>
        <welcome-file>index.jsp</welcome-file>
        <welcome-file>default.html</welcome-file>
        <welcome-file>default.htm</welcome-file>
        <welcome-file>default.jsp</welcome-file>
      </welcome-file-list>
     
       <servlet>
            <servlet-name>jersey-servlet</servlet-name>
            <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class>
            <init-param>
                 <param-name>jersey.config.server.provider.packages</param-name>
                 <param-value>my.first.rest</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
        </servlet>
     
        <servlet-mapping>
            <servlet-name>jersey-servlet</servlet-name>
            <url-pattern>/*</url-pattern>
        </servlet-mapping>
    </web-app>
  • Note that:
    1. The parameter jersey.config.server.provider.packages for <param-name> defines in which package Jersey will look for the web service classes
    2. <param-value> is your package's name.
    3. the URL of your service will be: http://your_domain:port/<display-name>/<url-pattern>/<path_from_rest_class>. In this example, this URL is: http://localhost:8080/rest/hello:
      • <display-name> in web.xml is rest
      • <url-pattern> in web.xml is empty (the * is a wild card, meaning any text)
      • <path_from_rest_class> is the path as defined in step 7

Step 4: Publish and check your service

  1. Start the Tomcat server
  2. Right click on Tomcat server→Add and Remove…. Select the rest project.
  3. Verify the response in HTML by your browser: http://localhost:8080/rest/hello.
  4. Verify the response in PLAINTEXT using command curl: open your terminal and type: curl http://localhost:8080/rest/hello
teaching_assistant/web_services/jersey_helloworld.txt · Last modified: 2023/09/06 07:50 by IT Courses (NNC)
Back to top
CC Attribution-Share Alike 4.0 International
chimeric.de = chi`s home Valid CSS Driven by DokuWiki do yourself a favour and use a real browser - get firefox!! Recent changes RSS feed Valid XHTML 1.0