From ea758837cc5486a74ac8ee63eb256625becbbeb9 Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Thu, 17 May 2018 23:28:12 +0200 Subject: [PATCH 1/8] code samples for BAEL-1789 Returning JSON object from a servlet --- javax-servlets/pom.xml | 91 +++++++++++-------- .../java/com/baeldung/model/Employee.java | 72 +++++++++++++++ .../baeldung/servlets/EmployeeServlet.java | 37 ++++++++ .../servlets/EmployeeServletTest.java | 60 ++++++++++++ 4 files changed, 221 insertions(+), 39 deletions(-) create mode 100644 javax-servlets/src/main/java/com/baeldung/model/Employee.java create mode 100644 javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java create mode 100644 javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index 69d952da9c..3970b392f2 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -1,46 +1,59 @@ - 4.0.0 - javax-servlets - 1.0-SNAPSHOT + xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> + 4.0.0 + javax-servlets + war + 1.0-SNAPSHOT - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + - - - javax.servlet - javax.servlet-api - ${javax.servlet.version} - - - org.apache.httpcomponents - httpclient - ${org.apache.httpcomponents.version} - test - - - commons-logging - commons-logging - - - - - org.springframework - spring-test - ${spring-test.version} - test - - + + + javax.servlet + javax.servlet-api + ${javax.servlet.version} + + + org.apache.httpcomponents + httpclient + ${org.apache.httpcomponents.version} + test + + + commons-logging + commons-logging + + + + + com.google.code.gson + gson + ${gson.version} + + + org.springframework + spring-test + ${spring-test.version} + test + + + org.mockito + mockito-core + 2.18.3 + test + + - - 3.1.0 - 4.5.3 - 5.0.5.RELEASE - + + 3.1.0 + 4.5.3 + 5.0.5.RELEASE + 2.8.4 + \ No newline at end of file diff --git a/javax-servlets/src/main/java/com/baeldung/model/Employee.java b/javax-servlets/src/main/java/com/baeldung/model/Employee.java new file mode 100644 index 0000000000..9a85a69fff --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/model/Employee.java @@ -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; + } + +} diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java new file mode 100644 index 0000000000..a29f440455 --- /dev/null +++ b/javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java @@ -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(); + } + +} diff --git a/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java b/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java new file mode 100644 index 0000000000..a821a30340 --- /dev/null +++ b/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java @@ -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); + } + +} From 8123ba644295368255c46285973bbad16776c304 Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Fri, 18 May 2018 08:36:46 +0200 Subject: [PATCH 2/8] corrected method name for test. --- .../test/java/com/baeldung/servlets/EmployeeServletTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java b/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java index a821a30340..89d907c347 100644 --- a/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java +++ b/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java @@ -30,7 +30,7 @@ public class EmployeeServletTest { Employee employee; @Test - public void whenPostRequestToEmployeeServlet_thenCorrect() throws Exception { + public void whenPostRequestToEmployeeServlet_thenEmployeeReturnedAsJson() throws Exception { //Given int id = 1; From e375a00a0962301b9b8e385b8ca26967ee357baf Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Sun, 20 May 2018 13:00:25 +0200 Subject: [PATCH 3/8] changes for servlet --- javax-servlets/pom.xml | 10 +++---- .../baeldung/servlets/EmployeeServlet.java | 30 +++++++++---------- 2 files changed, 20 insertions(+), 20 deletions(-) diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index 3970b392f2..4f9e18a900 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -18,6 +18,11 @@ javax.servlet-api ${javax.servlet.version} + + com.google.code.gson + gson + ${gson.version} + org.apache.httpcomponents httpclient @@ -30,11 +35,6 @@ - - com.google.code.gson - gson - ${gson.version} - org.springframework spring-test diff --git a/javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java b/javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java index a29f440455..ea82ca5055 100644 --- a/javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java +++ b/javax-servlets/src/main/java/com/baeldung/servlets/EmployeeServlet.java @@ -14,24 +14,24 @@ 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 { + 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(); + 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); + String employeeJsonString = new Gson().toJson(employee); + + PrintWriter out = response.getWriter(); + response.setContentType("application/json"); + response.setCharacterEncoding("UTF-8"); + out.print(employeeJsonString); + out.flush(); } } From fb9ab071a5843bb043e39596ce2b41fb30ed27d7 Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Sun, 20 May 2018 13:34:18 +0200 Subject: [PATCH 4/8] resolve merge confilct --- javax-servlets/pom.xml | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index 4f9e18a900..3970b392f2 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -18,11 +18,6 @@ javax.servlet-api ${javax.servlet.version} - - com.google.code.gson - gson - ${gson.version} - org.apache.httpcomponents httpclient @@ -35,6 +30,11 @@ + + com.google.code.gson + gson + ${gson.version} + org.springframework spring-test From 675f346e3d0159d233e452d79b1fa2f718c25d13 Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Sun, 20 May 2018 13:52:51 +0200 Subject: [PATCH 5/8] pom.xml from the master --- javax-servlets/pom.xml | 45 ++++++++++++++++++++++++++++-------------- 1 file changed, 30 insertions(+), 15 deletions(-) diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index 3970b392f2..5adc9da0b9 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -3,7 +3,6 @@ xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd"> 4.0.0 javax-servlets - war 1.0-SNAPSHOT @@ -13,11 +12,40 @@ + + + commons-fileupload + commons-fileupload + 1.3.3 + + + commons-io + commons-io + 2.6 + + + javax.servlet javax.servlet-api - ${javax.servlet.version} + 4.0.1 + + javax.servlet.jsp.jstl + jstl-api + 1.2 + + + javax.servlet.jsp + javax.servlet.jsp-api + 2.3.1 + + + javax.servlet + jstl + 1.2 + + org.apache.httpcomponents httpclient @@ -30,30 +58,17 @@ - - com.google.code.gson - gson - ${gson.version} - org.springframework spring-test ${spring-test.version} test - - org.mockito - mockito-core - 2.18.3 - test - - 3.1.0 4.5.3 5.0.5.RELEASE - 2.8.4 \ No newline at end of file From 93b61cd5116d829410c485bdfd79dbb3cbc2ef0f Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Sun, 20 May 2018 14:35:11 +0200 Subject: [PATCH 6/8] resolve merge conflicts --- javax-servlets/pom.xml | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index 5adc9da0b9..a33701fda9 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -58,6 +58,11 @@ + + com.google.code.gson + gson + ${gson.version} + org.springframework spring-test From b78584e5f97c5c8e2bd888ba660c7592773f0a96 Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Sun, 20 May 2018 17:00:11 +0200 Subject: [PATCH 7/8] resolved conflict --- javax-servlets/pom.xml | 77 +----------------------------------------- 1 file changed, 1 insertion(+), 76 deletions(-) diff --git a/javax-servlets/pom.xml b/javax-servlets/pom.xml index f1edebc7d3..f64ce67a1f 100644 --- a/javax-servlets/pom.xml +++ b/javax-servlets/pom.xml @@ -1,5 +1,4 @@ -<<<<<<< HEAD 4.0.0 @@ -75,80 +74,6 @@ 4.5.3 5.0.5.RELEASE + 2.8.2 -======= - - 4.0.0 - javax-servlets - 1.0-SNAPSHOT - - - com.baeldung - parent-modules - 1.0.0-SNAPSHOT - - - - - - commons-fileupload - commons-fileupload - 1.3.3 - - - commons-io - commons-io - 2.6 - - - - - javax.servlet - javax.servlet-api - 4.0.1 - - - javax.servlet.jsp.jstl - jstl-api - 1.2 - - - javax.servlet.jsp - javax.servlet.jsp-api - 2.3.1 - - - javax.servlet - jstl - 1.2 - - - - org.apache.httpcomponents - httpclient - ${org.apache.httpcomponents.version} - test - - - commons-logging - commons-logging - - - - - org.springframework - spring-test - ${spring-test.version} - test - - - - - 4.5.3 - 5.0.5.RELEASE - ->>>>>>> c2590b9d7ed2068984a248e213046f0cf34bdc30 - \ No newline at end of file From edc34bd157597ee06cfe79825b2d9ce553837dc1 Mon Sep 17 00:00:00 2001 From: Karan Khanna Date: Sun, 20 May 2018 17:04:21 +0200 Subject: [PATCH 8/8] formatted structure. --- .../servlets/EmployeeServletTest.java | 52 +++++++++---------- 1 file changed, 26 insertions(+), 26 deletions(-) diff --git a/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java b/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java index 89d907c347..2ebd58f50b 100644 --- a/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java +++ b/javax-servlets/src/test/java/com/baeldung/servlets/EmployeeServletTest.java @@ -20,41 +20,41 @@ import com.google.gson.Gson; @RunWith(MockitoJUnitRunner.class) public class EmployeeServletTest { - + @Mock HttpServletRequest httpServletRequest; - + @Mock HttpServletResponse httpServletResponse; - + Employee employee; - + @Test - public void whenPostRequestToEmployeeServlet_thenEmployeeReturnedAsJson() throws Exception { + public void whenPostRequestToEmployeeServlet_thenEmployeeReturnedAsJson() 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); + String name = "Karan Khanna"; + String department = "IT"; + Double salary = 5000.0; + employee = new Employee(id, name, department, salary); - 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); - } + //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); + } }