Thursday 17, Apr 2025
We are moved to new domain
Click -> www.ehowtonow.com
Saturday, 3 August 2013

Hello World RESTful Web service


Create new Dynamic Web Project and  give project name as RESTful_HelloWorld



Copy all jar files into WEB-INF/lib folder.Select build path add jar in your build path.

Create Class

Create package called com.javatutorialscorner.rest.ws
Create following class inside package com.javatutorialscorner.rest.ws
RestHelloWorld .java
01.package com.javatutorialscorner.rest.ws;
02. 
03.import javax.ws.rs.GET;
04.import javax.ws.rs.Path;
05.import javax.ws.rs.Produces;
06.import javax.ws.rs.core.MediaType;
07. 
08.@Path("/hello")
09.public class RestHelloWorld {
10.  
11. @GET
12. @Produces(MediaType.TEXT_PLAIN)
13. public String sayHello(){
14.  return "Welcome to Java Tutorials Corner";
15. }
16. 
17.}

This class called by using base url + path and get resource using HTTP GET method its is specified by annotation @GET and it produce the Sting output as  Plain Text output and @PATH define the class URI
now your project directory structure look like this 
project directory

Configure Jersey Servlet dispatcher

you need to register REST as servlet dispatcher in web.xml.Configure the following content in web.xml
web.xml
02.  <display-name>Restful_HelloWorld</display-name>
03.<servlet>
04.  <servlet-name>jersey-serlvet</servlet-name>
05.  <servlet-class>
06.                     com.sun.jersey.spi.container.servlet.ServletContainer
07.                </servlet-class>
08.  <init-param>
09.       <param-name>com.sun.jersey.config.property.packages</param-name>
10.       <param-value>com.javatutorialscorner.rest.ws</param-value>
11.  </init-param>
12.  <load-on-startup>1</load-on-startup>
13. </servlet>
14.  
15. <servlet-mapping>
16.  <servlet-name>jersey-serlvet</servlet-name>
17.  <url-pattern>/rest/*</url-pattern>
18. </servlet-mapping>
19.</web-app>

This is servlet class available in jersey com.sun.jersey.spi.container.servlet.ServletContainer.The init param com.sun.jersey.config.property.packages is used to defines in which package jersey will look for the web service classes.This package point to your resource class package.URL pattern is part of your base URL.Now your web service is read to run.Your Web Service available at following url
http://localhost:8080/RESTful_HelloWorld/rest/hello
http://your domain:port/Project Name/url pattern/path

Output

open browser enter the mention url you can see the following output
output
We can able to call GET method alone via browsers other methods of http should be called via client application

Create Client

you can create and run your client anywhere as per your wish in this example I created client inside same package
HelloWorldClient.java
01.package com.javatutorialscorner.rest.ws;
02. 
03.import javax.ws.rs.core.MediaType;
04. 
05.import com.sun.jersey.api.client.Client;
06.import com.sun.jersey.api.client.WebResource;
07.import com.sun.jersey.api.client.config.ClientConfig;
08.import com.sun.jersey.api.client.config.DefaultClientConfig;
09. 
10.public class HelloWorldClient {
11. 
12. public static void main(String[] args) {
13.  String baseURI = "http://localhost:8080/RESTful_HelloWorld";
14.  ClientConfig config = new DefaultClientConfig();
15.  Client client = Client.create(config);
16.  WebResource service = client.resource(baseURI);
17. 
18.  System.out.println(service.path("rest").path("hello")
19.    .accept(MediaType.TEXT_PLAIN).get(String.class));
20. 
21. }
22. 
23.}

If you run this client you will get the following out in console
Welcome to Java Tutorials Corner


Shop and help us

Flipkart Offer Snapdeal offer Amazon.in offer Amazon.com offer

Related Posts:

  • JAX-RS Get HTTP Headers using @HeaderParamIn this tutorial we are going to see how to get HTTP request header using JAX-RS  RESTful web service @HeaderParam 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project as given below. 2… Read More
  • JAX-RS @QueryParam with @DefaultValueIn this tutorial we are going to see how set  default value to query parameters in RESTful web service using @DefaultValue 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project.2. Cre… Read More
  • JAX-RS Get HTTP Headers using @ContextIn this tutorial we are going to see how to get HTTP request header using JAX-RS  RESTful web service @Context 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the Proje… Read More
  • Download PDF file using JAX-RS RESTful WebserviceIn this tutorial we are going to see how to download pf file using RESTful web service @Produces("application/pdf") annotation 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Crea… Read More
  • JAX-RS get QueryParam using @Context UriInfoIn this tutorial we are going to see how to retrieve  query parameters in RESTful web service using @Context UriInfo 1. Create new Dynamic web project by choosing File –> New –> Dynamic Web Project . 2. Create the… Read More
  • Blogger Comments
  • Facebook Comments
  • Disqus Comments

1 comments:

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Item Reviewed: Hello World RESTful Web service Rating: 5 Reviewed By: eHowToNow