JAX-RS Employee Management Service Sample
This page walks you through developing, deploying and testing a sample JAX-RS employee management service using Developer Studio. It includes the following sections:
Sample Scenario
This sample is about how to store and distribute employee information as a service for a company. In this sample service, information is stored in a HashMap. It is possible to register an employee, get information using the employee ID and update or modify information.
Implementing the service
- Open the Developer Studio Dashboard (click Developer Studio > Dashboard) and click JAX-RS Service Project.
 - Click Create New JAX-RS Service and then click Next.
- Enter EmployeeManagementJAXRSProject as the project name, com.employee.management.jaxrs.service as the package name and EmployeeManagementService as the class name.Â
 - Click Finish. The project structure is created.
Implement the service class and two beans classes for response messages, as shown below:
EmployeeManagementService.javapackage com.employee.management.jaxrs.service; import java.util.HashMap; import java.util.Map; import javax.ws.rs.*; import javax.ws.rs.core.Response; import com.employee.management.jaxrs.beans.Employee; import com.employee.management.jaxrs.beans.Message; @Path("/employee") public class EmployeeManagementService { public static Map<String, Map<String, String>> employees = new HashMap<>(); @POST @Consumes("text/plain") @Produces("text/xml") @Path("/insert/query") public Response insertEmployee(@QueryParam("id") String id, @QueryParam("name") String name, @QueryParam("designation") String designation, @QueryParam("salary") String salary) { id = id.trim(); if (!employees.containsKey(id)) { Map<String, String> info = new HashMap<>(); info.put("name", name); info.put("designation", designation); info.put("salary", salary); employees.put(id, info); Message msg = new Message(); msg.setMessage("Successfully registered"); return Response.ok(msg).build(); } Message msg = new Message(); msg.setMessage("Employee id already registered"); return Response.ok(msg).build(); } @POST @Consumes("text/plain") @Produces("text/xml") @Path("/update/designation") public Response updateEmployeeDesignation(@QueryParam("id") String id, @QueryParam("designation") String designation) { id = id.trim(); if (employees.containsKey(id)) { Map<String, String> info = new HashMap<>(); info = employees.get(id); info.put("designation", designation); Message msg = new Message(); msg.setMessage("Successfully updated"); return Response.ok(msg).build(); } Message msg = new Message(); msg.setMessage("Employee id is not registered"); return Response.ok(msg).build(); } @POST @Consumes("text/plain") @Produces("text/xml") @Path("/update/salary") public Response updateEmployeeSalary(@QueryParam("id") String id, @QueryParam("salary") String salary) { id = id.trim(); if (employees.containsKey(id)) { Map<String, String> info = new HashMap<>(); info = employees.get(id); info.put("salary", salary); Message msg = new Message(); msg.setMessage("Successfully updated"); return Response.ok(msg).build(); } Message msg = new Message(); msg.setMessage("Employee id is not registered"); return Response.ok(msg).build(); } @POST @Consumes("text/plain") @Produces("text/xml") @Path("/update/name") public Response updateEmployeeName(@QueryParam("id") String id, @QueryParam("name") String name) { id = id.trim(); if (employees.containsKey(id)) { Map<String, String> info = new HashMap<>(); info = employees.get(id); info.put("name", name); Message msg = new Message(); msg.setMessage("Successfully updated"); return Response.ok(msg).build(); } Message msg = new Message(); msg.setMessage("Employee id is not registered"); return Response.ok(msg).build(); } @GET @Consumes("text/plain") @Produces("text/xml") @Path("/get/{id}") public Response getEmployee(@PathParam("id") String id) { id = id.trim(); if (employees.containsKey(id)) { Map<String, String> info = new HashMap<>(); info = employees.get(id); Employee entity = new Employee(); entity.setId(id); entity.setName(info.get("name")); entity.setDesignation(info.get("designation")); entity.setSalary(info.get("salary")); return Response.ok(entity).build(); } Message msg = new Message(); msg.setMessage("ID is not registered"); return Response.ok(msg).build(); } }
Employee.javapackage com.employee.management.jaxrs.beans; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Employee") public class Employee { private String id; private String name; private String designation; private String salary; public String getId() { return id; } public void setId(String id) { this.id = id; } public String getName() { return name; } public void setName(String name) { this.name = name; } public String getDesignation() { return designation; } public void setDesignation(String designation) { this.designation = designation; } public String getSalary() { return salary; } public void setSalary(String salary) { this.salary = salary; } }
Message.javapackage com.employee.management.jaxrs.beans; import javax.xml.bind.annotation.XmlRootElement; @XmlRootElement(name = "Message") public class Message { private String message; public String getMessage() { return message; } public void setMessage(String message) { this.message = message; } }
Â
- Create a C-App project and deploy it to the Application Server.
Testing the service
You can test the sample service using the REST Easy extension from Firefox or Google Chrome.
Test the operations for registering an employee, update/modify and get information, as shown below.
Register employee
http://localhost:9763/EmployeeManagementJAXRSProject-1.0.0/services/employee_management_service/employee/insert/query?id=1&name=NAME&designation=DESIGNATION&salary=SALARY_AMOUNT
Get employee information
http://localhost:9763/EmployeeManagementJAXRSProject-1.0.0/services/employee_management_service/employee/get/index
Update employee name
http://localhost:9763/EmployeeManagementJAXRSProject-1.0.0/services/employee_management_service/employee/update/name?id=1&name=NAME_MODIFIED
Update employee designation
http://10.100.7.83:9763/EmployeeManagementJAXRSProject-1.0.0/services/employee_management_service/employee/update/designation?id=1&designation=DESIGNATION_MODIFIED
Update employee salary
http://10.100.7.83:9763/EmployeeManagementJAXRSProject-1.0.0/services/employee_management_service/employee/update/salary?id=1&salary=SALARY_MODIFIED
Information after modifications
Â