code samples for BAEL-1789 Returning JSON object from a servlet

This commit is contained in:
Karan Khanna 2018-05-17 23:28:12 +02:00
parent daafd33aac
commit ea758837cc
4 changed files with 221 additions and 39 deletions

View File

@ -3,6 +3,7 @@
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<artifactId>javax-servlets</artifactId>
<packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<parent>
@ -29,18 +30,30 @@
</exclusion>
</exclusions>
</dependency>
<dependency>
<groupId>com.google.code.gson</groupId>
<artifactId>gson</artifactId>
<version>${gson.version}</version>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring-test.version}</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>2.18.3</version>
<scope>test</scope>
</dependency>
</dependencies>
<properties>
<javax.servlet.version>3.1.0</javax.servlet.version>
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
<spring-test.version>5.0.5.RELEASE</spring-test.version>
<gson.version>2.8.4</gson.version>
</properties>
</project>

View File

@ -0,0 +1,72 @@
package com.baeldung.model;
public class Employee {
private int id;
private String name;
private String department;
private Double salary;
public Employee(int id, String name, String department, Double salary) {
super();
this.id = id;
this.name = name;
this.department = department;
this.salary = salary;
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result + id;
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
Employee other = (Employee) obj;
if (id != other.id)
return false;
return true;
}
public int getId() {
return id;
}
public void setId(int id) {
this.id = id;
}
public String getName() {
return name;
}
public void setName(String name) {
this.name = name;
}
public String getDepartment() {
return department;
}
public void setDepartment(String department) {
this.department = department;
}
public Double getSalary() {
return salary;
}
public void setSalary(Double salary) {
this.salary = salary;
}
}

View File

@ -0,0 +1,37 @@
package com.baeldung.servlets;
import java.io.IOException;
import java.io.PrintWriter;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import com.baeldung.model.Employee;
import com.google.gson.Gson;
@WebServlet(name = "EmployeeServlet", urlPatterns = "/employeeServlet")
public class EmployeeServlet extends HttpServlet {
@Override
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws IOException {
int id = Integer.parseInt(request.getParameter("id"));
String name = request.getParameter("name");
String department = request.getParameter("department");
Double salary = Double.parseDouble(request.getParameter("salary"));
Employee employee = new Employee(id, name, department, salary);
PrintWriter out = response.getWriter();
String employeeJsonString = new Gson().toJson(employee);
response.setContentType("application/json");
response.setCharacterEncoding("UTF-8");
out.print(employeeJsonString);
out.flush();
}
}

View File

@ -0,0 +1,60 @@
package com.baeldung.servlets;
import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.when;
import java.io.PrintWriter;
import java.io.StringWriter;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.Mock;
import org.mockito.junit.MockitoJUnitRunner;
import com.baeldung.model.Employee;
import com.google.gson.Gson;
@RunWith(MockitoJUnitRunner.class)
public class EmployeeServletTest {
@Mock
HttpServletRequest httpServletRequest;
@Mock
HttpServletResponse httpServletResponse;
Employee employee;
@Test
public void whenPostRequestToEmployeeServlet_thenCorrect() throws Exception {
//Given
int id = 1;
String name = "Karan Khanna";
String department = "IT";
Double salary = 5000.0;
employee = new Employee(id, name, department, salary);
//when
when(httpServletRequest.getParameter("id")).thenReturn(String.valueOf(id));
when(httpServletRequest.getParameter("name")).thenReturn(name);
when(httpServletRequest.getParameter("department")).thenReturn(department);
when(httpServletRequest.getParameter("salary")).thenReturn(String.valueOf(salary));
StringWriter sw = new StringWriter();
PrintWriter pw = new PrintWriter(sw);
when(httpServletResponse.getWriter()).thenReturn(pw);
EmployeeServlet employeeServlet = new EmployeeServlet();
employeeServlet.doPost(httpServletRequest, httpServletResponse);
String employeeJsonString = sw.getBuffer().toString().trim();
Employee fetchedEmployee = new Gson().fromJson(employeeJsonString, Employee.class);
assertEquals(fetchedEmployee, employee);
}
}