Last active
August 29, 2015 14:16
-
-
Save thescouser89/13585e71ea4affd8c8cc to your computer and use it in GitHub Desktop.
Gson Test
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?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" | |
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> | |
<groupId>gson-play</groupId> | |
<artifactId>gson-play</artifactId> | |
<version>1.0-SNAPSHOT</version> | |
<dependencies> | |
<dependency> | |
<groupId>com.google.code.gson</groupId> | |
<artifactId>gson</artifactId> | |
<version>2.3.1</version> | |
</dependency> | |
<dependency> | |
<groupId>javax.servlet</groupId> | |
<artifactId>javax.servlet-api</artifactId> | |
<version>3.0.1</version> | |
</dependency> | |
</dependencies> | |
</project> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import com.google.gson.Gson; | |
import javax.servlet.ServletException; | |
import javax.servlet.http.HttpServletRequest; | |
import javax.servlet.http.HttpServletResponse; | |
import java.io.IOException; | |
import java.io.PrintWriter; | |
import java.util.HashMap; | |
/** | |
* Created by dcheung on 2/25/15. | |
*/ | |
public class GsonPlay { | |
public static void main(String[] args) { | |
Gson gson = new Gson(); | |
String[] strings = {"abc", "def", "ghi"}; | |
HashMap<String, String> test = new HashMap<String, String>(); | |
test.put("test", "hello"); | |
test.put("boo", "pie"); | |
System.out.println(gson.toJson(test)); | |
} | |
public void doGet(HttpServletRequest request, | |
HttpServletResponse response) | |
throws ServletException, IOException { | |
Gson gson = new Gson(); | |
String[] strings = {"abc", "def", "ghi"}; | |
// Set response content type | |
response.setContentType("application/json"); | |
// Actual logic goes here. | |
PrintWriter out = response.getWriter(); | |
out.println(gson.toJson(strings)); | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<servlet> | |
<servlet-name>GsonTest</servlet-name> | |
<servlet-class>ca.utoronto.GsonTest</servlet-class> | |
</servlet> | |
<servlet-mapping> | |
<servlet-name>GsonTest</servlet-name> | |
<url-pattern>/getGsonData</url-pattern> | |
</servlet-mapping> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment