rest helloworld

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

  • Eclipse Java EE.
  • Tomcat server.
  • Jersey library.

Step 1: setting up Tomcat on Eclipse

  • Refer to  this tutorial.

Step 2: Create your project

  1. Open  EclipseFile→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  jersey-server.jar ,  jersey-core.jarjersey-servlet.jar and  asm.jar 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")
    public class Hello {
     
      // This method is called if TEXT_PLAIN is requested
      @GET
      @Produces(MediaType.TEXT_PLAIN)
      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://java.sun.com/xml/ns/javaee" 
    xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 
    xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd" 
    id="WebApp_ID" version="3.0">
     
      <display-name>rest</display-name>
     
      <servlet>
        <servlet-name>Jersey REST Service</servlet-name>
        <servlet-class>com.sun.jersey.spi.container.servlet.ServletContainer</servlet-class>
        <init-param>
          <param-name>com.sun.jersey.config.property.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 REST Service</servlet-name>
        <url-pattern>/*</url-pattern>
      </servlet-mapping>
     
    </web-app>
  • Note that:
    1. <param-value> is your package's name.
    2. the  URL of your service will be:  http://your_domain:port/<project-name>/<url-pattern>/<path_from_rest_class>. In this example, this  URL is:  http://localhost:8080/rest/hello (<project-name> is  rest, <url-pattern> is empty, <path_from_ rest_class> is hello as declared in step 2).

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

你可能感兴趣的:(rest helloworld)