Develop JAX-RS Web Service Easily Using WSO2 Developer Studio

Develop JAX-RS Web Service Easily Using WSO2 Developer Studio

INTRODUCTION

This post will guide you to develop a JAX-RS Web Service using WSO2 Developer Studio and host it in WSO2 Application server and test it.

First if you don’t have WSO2 Developer Studio installed, install developer studio. We will use WSO2 Application Server to host our application and test it by using Rest Easy extension in Firefox or Google Chrome.

SAMPLE SCENARIO

We will implement an employee information web service for a company. In this sample service, information is stored in a HashMap. This web service is able to register an employee, get information using the employee ID and update or modify information.

IMPLEMENTING SERVICE CLASSES

  1. Open the Developer Studio Dashboard and click JAX-RS Service Project.
  2. Click Create New JAX-RS Service and then click Next.
  3. Enter EmployeeManagementJAXRSProject as the project name, com.employee.management.jaxrs.service as the package name and EmployeeManagementService as the class name.
  4. Click Finish. The project structure is created.
  5. Implement the service class and two bean classes for response messages, as shown below:
EmployeeManagementService.java
package 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.java
package 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.java
package 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;
    }
    
}

6 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

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s