By
using javax.ws.rs.core.Application class, we can configure resources and
providers in rest application.
Application
class provides below methods to define resources.
Method
|
Description
|
public
Set<Class<?>> getClasses()
|
Return
a set of root resource and provider classes. For every request, new instance
of the resource is created.
|
public
Set<Object> getSingletons()
|
Return
a set of root resource and provider classes. There is one instance of the
resource is created and shared across all the requests.
|
Map<String,
Object> getProperties()
|
Return
a map of custom application-wide properties.
|
In
a typical application, you extend javax.ws.rs.core.Application class and
provide all the resource classes in getClasses (or) getSingletons method.
public class RestApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> s = new HashSet<Class<?>>(); s.add(EmployeeResource.class); return s; } }
Below
step-by-step procedure explains the usage of ‘javax.ws.rs.core.Application’
class.
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.model’ and define the class Employee like below.
Employee.java
package com.sample.model; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement public class Employee { private int id; private String firstName; private String lastName; public int getId() { return id; } public void setId(int id) { this.id = id; } public String getFirstName() { return firstName; } public void setFirstName(String firstName) { this.firstName = firstName; } public String getLastName() { return lastName; } public void setLastName(String lastName) { this.lastName = lastName; } }
Step 4: Create a package 'com.sample.service'
and define EmployeeService class.
EmployeeService.java
package com.sample.service; import java.util.HashMap; import java.util.Map; import com.sample.model.Employee; public class EmployeeService { private static Map<Integer, Employee> employees = new HashMap<>(); private static int id = 1; static { initializeEmployees(); } private static void initializeEmployees() { Employee emp1 = new Employee(); emp1.setFirstName("Chamu"); emp1.setLastName("Majety"); emp1.setId(id); id++; Employee emp2 = new Employee(); emp2.setFirstName("Siva"); emp2.setLastName("Ponnam"); emp2.setId(id); id++; employees.put(emp1.getId(), emp1); employees.put(emp2.getId(), emp2); } public static Map<Integer, Employee> getAllEmployees() { return employees; } public static Employee getEmployee(int id) { Employee emp = employees.get(id); return emp; } public static Employee createEmployee(Employee employee) { Employee emp = new Employee(); emp.setFirstName(employee.getFirstName()); emp.setLastName(employee.getLastName()); emp.setId(id); id++; return emp; } }
Step 5: Create a package ‘com.sample.resources´and
define EmployeeResource class like below.
EmployeeResource.java
package com.sample.resources; import java.util.ArrayList; import java.util.List; import javax.ws.rs.GET; import javax.ws.rs.POST; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import javax.ws.rs.core.MediaType; import com.sample.model.Employee; import com.sample.service.EmployeeService; @Path("employees") public class EmployeeResource { @GET @Produces(MediaType.APPLICATION_JSON) public List<Employee> getAllEmployees() { return new ArrayList<>(EmployeeService.getAllEmployees().values()); } @GET @Path("/{employeeId}") @Produces(MediaType.APPLICATION_JSON) public Employee getEmployee(@PathParam("employeeId") int empId) { Employee emp = EmployeeService.getEmployee(empId); return emp; } @POST @Produces(MediaType.APPLICATION_JSON) public Employee createEmployee(Employee emp) { Employee employee = EmployeeService.createEmployee(emp); return employee; } }
Step 6: Create a package ‘com.sample.rest´and
define the class ‘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.EmployeeResource; public class RestApplication extends Application { @Override public Set<Class<?>> getClasses() { Set<Class<?>> s = new HashSet<Class<?>>(); s.add(EmployeeResource.class); return s; } }
Step 7: Define web.xml file
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>
As
you see below servlet definition ’com.sample.rest.RestApplication’ is passed as
value to the init parameter ‘javax.ws.rs.Application’.
<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>
</servlet>
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, how to configure server in eclipse, you can go through my
below post.
Once
the application run on server, you can hit below url, to get list of employees.
http://localhost:8080/app/employees
Hit
below url to get employee wit id 1.
No comments:
Post a Comment