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

@ -1,46 +1,59 @@
<?xml version="1.0" encoding="UTF-8"?> <?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 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> <modelVersion>4.0.0</modelVersion>
<artifactId>javax-servlets</artifactId> <artifactId>javax-servlets</artifactId>
<version>1.0-SNAPSHOT</version> <packaging>war</packaging>
<version>1.0-SNAPSHOT</version>
<parent> <parent>
<groupId>com.baeldung</groupId> <groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId> <artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version> <version>1.0.0-SNAPSHOT</version>
</parent> </parent>
<dependencies> <dependencies>
<dependency> <dependency>
<groupId>javax.servlet</groupId> <groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId> <artifactId>javax.servlet-api</artifactId>
<version>${javax.servlet.version}</version> <version>${javax.servlet.version}</version>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.apache.httpcomponents</groupId> <groupId>org.apache.httpcomponents</groupId>
<artifactId>httpclient</artifactId> <artifactId>httpclient</artifactId>
<version>${org.apache.httpcomponents.version}</version> <version>${org.apache.httpcomponents.version}</version>
<scope>test</scope> <scope>test</scope>
<exclusions> <exclusions>
<exclusion> <exclusion>
<artifactId>commons-logging</artifactId> <artifactId>commons-logging</artifactId>
<groupId>commons-logging</groupId> <groupId>commons-logging</groupId>
</exclusion> </exclusion>
</exclusions> </exclusions>
</dependency> </dependency>
<dependency> <dependency>
<groupId>org.springframework</groupId> <groupId>com.google.code.gson</groupId>
<artifactId>spring-test</artifactId> <artifactId>gson</artifactId>
<version>${spring-test.version}</version> <version>${gson.version}</version>
<scope>test</scope> </dependency>
</dependency> <dependency>
</dependencies> <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> <properties>
<javax.servlet.version>3.1.0</javax.servlet.version> <javax.servlet.version>3.1.0</javax.servlet.version>
<org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version> <org.apache.httpcomponents.version>4.5.3</org.apache.httpcomponents.version>
<spring-test.version>5.0.5.RELEASE</spring-test.version> <spring-test.version>5.0.5.RELEASE</spring-test.version>
</properties> <gson.version>2.8.4</gson.version>
</properties>
</project> </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);
}
}