Browse Source

[ci fw-only Java/servlet] Add custom Jackson serialization (#3182)

* Add JSON test with Jackson custom serialization

* Update README.md
Radoslav Petrov 7 years ago
parent
commit
11dbb42a45

+ 5 - 0
frameworks/Java/servlet/README.md

@@ -7,6 +7,7 @@ This is the Java Servlet portion of a [benchmarking test suite](../) comparing a
 * [Plaintext test source](src/main/java/hello/PlaintextServlet.java)
 * [JSON test source](src/main/java/hello/JsonServlet.java)
 * [JSON test source with Jackson Afterburner module](src/main/java/hello/JsonAfterburnerServlet.java)
+* [JSON test source with custom Jackson serialization](src/main/java/hell/JsonCJSServlet.java)
 
 ### `MySQL` implementation
 
@@ -46,6 +47,10 @@ Please confirm the versions data with the latest install scripts of TFB project.
 
  * JSON - `http://localhost:8080/servlet/json`
 
+### `cjs` Maven profile
+
+ * JSON - `http://localhost:8080/servlet/json`
+
 ### `mysql` and `postgresql` Maven profiles
 
  * DB - `http://localhost:8080/servlet/db`

+ 19 - 0
frameworks/Java/servlet/benchmark_config.json

@@ -40,6 +40,25 @@
       "notes": "Jackson with Afterburner module",
       "versus": "servlet"
     },
+    "cjs": {
+      "setup_file": "setup_cjs",
+      "json_url": "/servlet/json",
+      "port": 8080,
+      "approach": "Realistic",
+      "classification": "Platform",
+      "database": "None",
+      "framework": "None",
+      "language": "Java",
+      "flavor": "None",
+      "orm": "Raw",
+      "platform": "Servlet",
+      "webserver": "Resin",
+      "os": "Linux",
+      "database_os": "Linux",
+      "display_name": "servlet",
+      "notes": "Jackson with custom serialization",
+      "versus": "servlet"
+    },
     "raw": {
       "setup_file": "setup_mysql",
       "db_url": "/servlet/db",

+ 28 - 1
frameworks/Java/servlet/pom.xml

@@ -29,7 +29,7 @@
 			<artifactId>jackson-module-afterburner</artifactId>
 			<version>${jackson-version}</version>
 		</dependency>
-		
+
 		<dependency>
 			<groupId>org.apache.taglibs</groupId>
 			<artifactId>taglibs-standard-impl</artifactId>
@@ -92,6 +92,33 @@
 				</plugins>
 			</build>
 		</profile>
+
+		<profile>
+			<id>cjs</id>
+			<properties>
+				<maven.war.xml>src/main/resources/WEB-INF/cjs/web.xml</maven.war.xml>
+			</properties>
+			<build>
+				<plugins>
+					<plugin>
+						<groupId>org.apache.maven.plugins</groupId>
+						<artifactId>maven-war-plugin</artifactId>
+						<configuration>
+							<webResources>
+								<resource>
+									<directory>src/main/resources/WEB-INF/cjs</directory>
+									<excludes>
+										<exclude>src/main/resources/WEB-INF/</exclude>
+									</excludes>
+									<targetPath>WEB-INF</targetPath>
+								</resource>
+							</webResources>
+						</configuration>
+					</plugin>
+				</plugins>
+			</build>
+		</profile>
+
 		<profile>
 			<id>mysql</id>
 			<properties>

+ 8 - 0
frameworks/Java/servlet/setup_cjs.sh

@@ -0,0 +1,8 @@
+#!/bin/bash
+
+fw_depends java resin maven
+
+mvn clean compile war:war -P cjs
+rm -rf $RESIN_HOME/webapps/*
+cp target/servlet.war $RESIN_HOME/webapps/
+resinctl start

+ 21 - 0
frameworks/Java/servlet/src/main/java/hello/Common.java

@@ -1,5 +1,6 @@
 package hello;
 
+import java.io.IOException;
 import java.sql.Connection;
 import java.sql.PreparedStatement;
 import java.sql.ResultSet;
@@ -7,7 +8,9 @@ import java.sql.SQLException;
 import java.util.HashMap;
 import java.util.Map;
 
+import com.fasterxml.jackson.core.JsonGenerator;
 import com.fasterxml.jackson.databind.*;
+import com.fasterxml.jackson.databind.jsontype.TypeSerializer;
 import com.fasterxml.jackson.module.afterburner.AfterburnerModule;
 
 /**
@@ -32,6 +35,24 @@ public class Common {
 		public final String message = "Hello, World!";
 	}
 	
+	// Response message class with custom Jackson serializer
+	public static class HelloMessageCJS implements JsonSerializable {
+		public final String message = "Hello, World!";
+		
+		@Override
+		public void serialize(JsonGenerator jg, SerializerProvider sp) throws IOException {
+			jg.writeStartObject();
+			jg.writeStringField("message", this.message);
+			jg.writeEndObject();
+		}
+
+		@Override
+		public void serializeWithType(JsonGenerator jg, SerializerProvider sp,
+				TypeSerializer ts) throws IOException {
+			throw new UnsupportedOperationException("Not needed so far.");
+		}
+	}
+	
 	public static int normalise(String param) {
 		int count = 1;
 		try {

+ 24 - 0
frameworks/Java/servlet/src/main/java/hello/JsonCJSServlet.java

@@ -0,0 +1,24 @@
+package hello;
+
+import java.io.IOException;
+
+import javax.servlet.ServletException;
+import javax.servlet.http.HttpServlet;
+import javax.servlet.http.HttpServletRequest;
+import javax.servlet.http.HttpServletResponse;
+
+/**
+ * JSON Encoding Test
+ */
+@SuppressWarnings("serial")
+public class JsonCJSServlet extends HttpServlet {
+	@Override
+	protected void doGet(HttpServletRequest req, HttpServletResponse res) throws ServletException,
+			IOException {
+		// Set content type to JSON
+		res.setHeader(Common.HEADER_CONTENT_TYPE, Common.CONTENT_TYPE_JSON);
+
+		// Write JSON encoded message to the response using the class with custom Jackson serialization.
+		Common.MAPPER.writeValue(res.getOutputStream(), new Common.HelloMessageCJS());
+	}
+}

+ 15 - 0
frameworks/Java/servlet/src/main/resources/WEB-INF/cjs/web.xml

@@ -0,0 +1,15 @@
+<?xml version="1.0" encoding="UTF-8" ?>
+
+<web-app xmlns="http://java.sun.com/xml/ns/j2ee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
+	xsi:schemaLocation="http://java.sun.com/xml/ns/j2ee http://java.sun.com/xml/ns/j2ee/web-app_2_4.xsd"
+	version="2.4">
+	<servlet>
+		<servlet-name>json</servlet-name>
+		<servlet-class>hello.JsonCJSServlet</servlet-class>
+		<load-on-startup>1</load-on-startup>
+	</servlet>
+	<servlet-mapping>
+		<servlet-name>json</servlet-name>
+		<url-pattern>/json</url-pattern>
+	</servlet-mapping>
+</web-app>