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.wsCreate 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

Configure Jersey Servlet dispatcher
you need to register REST as servlet dispatcher in web.xml.Configure the following content in web.xmlweb.xml
01.
<
web-app
id
=
"WebApp_ID"
version
=
"3.0"
xmlns:web
=
"http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd"
xmlns:xsi
=
"http://www.w3.org/2001/XMLSchema-instance"
xmlns
=
"http://java.sun.com/xml/ns/javaee"
xsi:schemalocation
=
"http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
>
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
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 packageHelloWorldClient.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
This comment has been removed by a blog administrator.
ReplyDelete