Step 1) Create Dynamic Web Application “RESTWebApp“.
Step 2) Create web.xml (deployment descriptor) under WebRootWEB-INF .
Step 3) Open web.xml file and add below code just above :
<?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" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" 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>RESTWebApp</display-name> <servlet> <servlet-name>jersey-serlvet</servlet-name> <servlet-class> com.sun.jersey.spi.container.servlet.ServletContainer </servlet-class> <load-on-startup>1</load-on-startup> </servlet> <servlet-mapping> <servlet-name>jersey-serlvet</servlet-name> <url-pattern>/doj/*</url-pattern> </servlet-mapping> </web-app>
Step 4) Adding two jars file You can download from here: asm-3.3.1.jar, jersey-bundle-1.14.jar
Step 5) Creating Employee bean class
Employee.java
package com.dineshonjava.ws.rest.bean; import javax.xml.bind.annotation.XmlElement; import javax.xml.bind.annotation.XmlRootElement; /** * @author Dinesh Rajput * */ @XmlRootElement(name = "employee") public class Employee { private String employeeId; private String employeeName; private String jobType; private String address; private Long salary; @XmlElement public String getEmployeeId() { return employeeId; } public void setEmployeeId(String employeeId) { this.employeeId = employeeId; } @XmlElement public String getEmployeeName() { return employeeName; } public void setEmployeeName(String employeeName) { this.employeeName = employeeName; } @XmlElement public String getAddress() { return address; } public void setAddress(String address) { this.address = address; } @XmlElement public Long getSalary() { return salary; } public void setSalary(Long salary) { this.salary = salary; } @XmlElement public String getJobType() { return jobType; } public void setJobType(String jobType) { this.jobType = jobType; } }
Step 6) Create WebController.java file
package com.dineshonjava.ws.rest; import java.util.ArrayList; import java.util.HashMap; import java.util.List; import java.util.Map; import javax.ws.rs.GET; import javax.ws.rs.Path; import javax.ws.rs.PathParam; import javax.ws.rs.Produces; import com.dineshonjava.ws.rest.bean.Employee; /** * @author Dinesh Rajput * */ @Path("/webservice") public class WebController { private static Map<String, Employee> employees = new HashMap<String, Employee>(); static { Employee employee1 = new Employee(); employee1.setEmployeeId("11111"); employee1.setEmployeeName("Dineh Rajput"); employee1.setJobType("Sr.Software Engineer"); employee1.setSalary(70000l); employee1.setAddress("Noida"); employees.put(employee1.getEmployeeId(), employee1); Employee employee2 = new Employee(); employee2.setEmployeeId("22222"); employee2.setEmployeeName("Abhishek"); employee2.setJobType("Marketing"); employee2.setSalary(50000l); employee2.setAddress("New Delhi"); employees.put(employee2.getEmployeeId(), employee2); } @GET @Path("/hello") @Produces("text/plain") public String hello(){ return "Hello World!!! dineshonjava"; } @GET @Path("/message/{message}") @Produces("text/plain") public String showMsg(@PathParam("message") String message){ return message; } @GET @Path("/employees") @Produces("application/xml") public List<Employee> listEmployees(){ return new ArrayList<Employee>(employees.values()); } @GET @Path("/employee/{employeeid}") @Produces("application/xml") public Employee getEmployee(@PathParam("employeeid")String employeeId){ return employees.get(employeeId); } @GET @Path("/json/employees/") @Produces("application/json") public List<Employee> listEmployeesJSON(){ return new ArrayList<Employee>(employees.values()); } @GET @Path("/json/employee/{employeeid}") @Produces("application/json") public Employee getEmployeeJSON(@PathParam("employeeid")String employeeId){ return employees.get(employeeId); } }
Step 7) Deploy project “RESTWebApp” on Tomcat7. Web project should be deployed without any exception.
Step 8) If every thing fine then test it now following link.
http://localhost:8181/sdnext/doj/webservice/hello
http://localhost:8181/sdnext/doj/webservice/message/Welcome%20to%20DineshOnJava.com
http://localhost:8181/sdnext/doj/webservice/employees
http://localhost:8181/sdnext/doj/webservice/employee/11111
http://localhost:8181/sdnext/doj/webservice/json/employees
http://localhost:8181/sdnext/doj/webservice/json/employee/11111
References
1. JAVA REST Web Services
2. Wikipedia for REST Web Service
Strategy Design Patterns We can easily create a strategy design pattern using lambda. To implement…
Decorator Pattern A decorator pattern allows a user to add new functionality to an existing…
Delegating pattern In software engineering, the delegation pattern is an object-oriented design pattern that allows…
Technology has emerged a lot in the last decade, and now we have artificial intelligence;…
Managing a database is becoming increasingly complex now due to the vast amount of data…
Overview In this article, we will explore Spring Scheduler how we could use it by…