In
my previous post, I explained, how to configure your rest resources by
extending ‘javax.ws.rs.core.Application’
class.
‘javax.ws.rs.core.Application’ class
provides getClasses and getSingletons methods.
If
you define any resource in getClasses method, it creates a new instance for
each new request. Whereas if you define any resource in getSingletons method,
it creates single instance of the resource and reuse the resource for all the
requests.
Let’s
see it by an example.
Step 1: Create new dynamic
web project ‘app’ in eclipse.
Right
click on ‘Project Explorer’ -> New -> Dynamic Web Project
Give
the project name as ‘app’ and press Next.
In
Java window, press Next.
In
Web Module window, select the check box ‘Generate web.xml deployment
descriptor’, press Finish.
Step 2: Mavenize the project.
Right
click on the project ‘app’ -> Configure -> Convert To Maven Project.
It
opens ‘Maven POM’ window, use the defaults and press the button Finish.
I
am using below maven dependencies.
<dependencies> <dependency> <groupId>org.glassfish.jersey.containers</groupId> <artifactId>jersey-container-servlet-core</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.core</groupId> <artifactId>jersey-server</artifactId> <version>2.25.1</version> </dependency> <dependency> <groupId>org.glassfish.jersey.media</groupId> <artifactId>jersey-media-moxy</artifactId> <version>2.25.1</version> </dependency> </dependencies>
Step 3: Create a package ‘com.sample.resources’
and define HelloWorldSingletonResource, HelloWorldResource classes like below.
HelloWorldSingletonResource.java
package com.sample.resources; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("HelloWorldSingletonResource") public class HelloWorldSingletonResource { private static int count = 0; public HelloWorldSingletonResource() { count++; } @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "Hello called " + count + " times"; } }
HelloWorldResource.java
package com.sample.resources; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; @Path("HelloWorldResource") public class HelloWorldResource { private static int count = 0; public HelloWorldResource() { count++; } @GET @Produces(MediaType.TEXT_PLAIN) public String hello() { return "HelloWorldResource called " + count + " times"; } }
Step 4: Create a package
'com.sample.rest' and define RestApplication like below.
RestApplication.java
package com.sample.rest; import java.util.HashSet; import java.util.Set; import javax.ws.rs.core.Application; import com.sample.resources.HelloWorldResource; import com.sample.resources.HelloWorldSingletonResource; public class RestApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> s = new HashSet<Class<?>>(); s.add(HelloWorldResource.class); return s; } public Set<Object> getSingletons() { Set<Object> set = new HashSet<>(); HelloWorldSingletonResource resource = new HelloWorldSingletonResource(); set.add(resource); return set; } }
Step 5: Define web.xml like
below.
web.xml
<?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" 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>jersey_tutorial</display-name> <servlet> <servlet-name>Jersey REST Service</servlet-name> <servlet-class>org.glassfish.jersey.servlet.ServletContainer</servlet-class> <init-param> <param-name>javax.ws.rs.Application</param-name> <param-value>com.sample.rest.RestApplication</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>
Complete
project structure looks like below.
Run
the application on server. Right click on the project ‘app’ -> Run As ->
Run on Server.
If
you do not know, hot to configure server in eclipse, you can go through my
below post.
Once
the application run on server, hit below url.
Re
hit the same url.
You
can see that the counter is getting increased, whenever you hit the url.
Now
hit the url, http://localhost:8080/app/HelloWorldSingletonResource,
try to hit the url as many times you want, the counter will not increase.
No comments:
Post a Comment