Skip to content

Commit 05e0169

Browse files
committed
servlet json
servlet json
1 parent 568f012 commit 05e0169

File tree

12 files changed

+6469
-0
lines changed

12 files changed

+6469
-0
lines changed

Diff for: java-servlet-json/.classpath

+11
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<classpath>
3+
<classpathentry kind="src" path="src/test/java" output="target/test-classes" including="**/*.java"/>
4+
<classpathentry kind="src" path="src/main/java" including="**/*.java"/>
5+
<classpathentry kind="output" path="target/classes"/>
6+
<classpathentry kind="var" path="M2_REPO/javax/servlet/javax.servlet-api/3.1.0/javax.servlet-api-3.1.0.jar"/>
7+
<classpathentry kind="con" path="org.eclipse.jdt.launching.JRE_CONTAINER"/>
8+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-core/2.2.2/jackson-core-2.2.2.jar"/>
9+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-databind/2.2.2/jackson-databind-2.2.2.jar"/>
10+
<classpathentry kind="var" path="M2_REPO/com/fasterxml/jackson/core/jackson-annotations/2.2.2/jackson-annotations-2.2.2.jar"/>
11+
</classpath>

Diff for: java-servlet-json/pom.xml

+50
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
2+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
3+
<modelVersion>4.0.0</modelVersion>
4+
5+
<groupId>com.hmkcode</groupId>
6+
<artifactId>java-servlet-json</artifactId>
7+
<version>1.0-SNAPSHOT</version>
8+
<packaging>jar</packaging>
9+
10+
<name>java-servlet-json</name>
11+
<url>http://maven.apache.org</url>
12+
13+
<properties>
14+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
15+
</properties>
16+
17+
<dependencies>
18+
<dependency>
19+
<groupId>javax.servlet</groupId>
20+
<artifactId>javax.servlet-api</artifactId>
21+
<version>3.1.0</version>
22+
</dependency>
23+
<dependency>
24+
<groupId>com.fasterxml.jackson.core</groupId>
25+
<artifactId>jackson-core</artifactId>
26+
<version>2.2.2</version>
27+
</dependency>
28+
<dependency>
29+
<groupId>com.fasterxml.jackson.core</groupId>
30+
<artifactId>jackson-databind</artifactId>
31+
<version>2.2.2</version>
32+
</dependency>
33+
<dependency>
34+
<groupId>com.fasterxml.jackson.core</groupId>
35+
<artifactId>jackson-annotations</artifactId>
36+
<version>2.2.2</version>
37+
</dependency>
38+
</dependencies>
39+
40+
<build>
41+
<finalName>java-servlet-json</finalName>
42+
<plugins>
43+
<plugin>
44+
<groupId>org.eclipse.jetty</groupId>
45+
<artifactId>jetty-maven-plugin</artifactId>
46+
<version>9.0.4.v20130625</version>
47+
</plugin>
48+
</plugins>
49+
</build>
50+
</project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.hmkcode;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.util.LinkedList;
7+
import java.util.List;
8+
import javax.servlet.ServletException;
9+
import javax.servlet.http.HttpServlet;
10+
import javax.servlet.http.HttpServletRequest;
11+
import javax.servlet.http.HttpServletResponse;
12+
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
import com.hmkcode.vo.Article;
15+
16+
public class JSONServlet extends HttpServlet {
17+
18+
private static final long serialVersionUID = 1L;
19+
20+
// This will store all received articles
21+
List<Article> articles = new LinkedList<Article>();
22+
23+
/***************************************************
24+
* URL: /jsonservlet
25+
* doPost(): receives JSON data, parse it, map it and send back as JSON
26+
****************************************************/
27+
protected void doPost(HttpServletRequest request, HttpServletResponse response)
28+
throws ServletException, IOException{
29+
30+
// 1. get received JSON data from request
31+
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
32+
String json = "";
33+
if(br != null){
34+
json = br.readLine();
35+
}
36+
37+
// 2. initiate jackson mapper
38+
ObjectMapper mapper = new ObjectMapper();
39+
40+
// 3. Convert received JSON to Article
41+
Article article = mapper.readValue(json, Article.class);
42+
43+
// 4. Set response type to JSON
44+
response.setContentType("application/json");
45+
46+
// 5. Add article to List<Article>
47+
articles.add(article);
48+
49+
// 6. Send List<Article> as JSON to client
50+
mapper.writeValue(response.getOutputStream(), articles);
51+
}
52+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package com.hmkcode.vo;
2+
3+
import java.util.LinkedList;
4+
import java.util.List;
5+
6+
public class Article {
7+
8+
private String title;
9+
private String url;
10+
private List<String> categories;
11+
private List<String> tags;
12+
13+
public String getTitle() {
14+
return title;
15+
}
16+
public void setTitle(String title) {
17+
this.title = title;
18+
}
19+
public String getUrl() {
20+
return url;
21+
}
22+
public void setUrl(String url) {
23+
this.url = url;
24+
}
25+
public List<String> getCategories() {
26+
return categories;
27+
}
28+
public void setCategories(List<String> categories) {
29+
this.categories = categories;
30+
}
31+
public List<String> getTags() {
32+
return tags;
33+
}
34+
public void setTags(List<String> tags) {
35+
this.tags = tags;
36+
}
37+
38+
public void addCategory(String category){
39+
if(this.categories == null)
40+
this.categories = new LinkedList<String>();
41+
this.categories.add(category);
42+
}
43+
public void addTag(String tag){
44+
if(this.tags == null)
45+
this.tags = new LinkedList<String>();
46+
47+
this.tags.add(tag);
48+
}
49+
@Override
50+
public String toString() {
51+
return "Article [title=" + title + ", url=" + url + ", categories="
52+
+ categories + ", tags=" + tags + "]";
53+
}
54+
55+
56+
57+
}

Diff for: java-servlet-json/src/main/webapp/WEB-INF/web.xml

+20
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xmlns="http://java.sun.com/xml/ns/javaee"
4+
xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
5+
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
6+
id="WebApp_ID" version="3.0">
7+
<display-name>Java Servlet JSON</display-name>
8+
<welcome-file-list>
9+
<welcome-file>index.html</welcome-file>
10+
</welcome-file-list>
11+
12+
<servlet>
13+
<servlet-name>jsonservlet</servlet-name>
14+
<servlet-class>com.hmkcode.JSONServlet</servlet-class>
15+
</servlet>
16+
<servlet-mapping>
17+
<servlet-name>jsonservlet</servlet-name>
18+
<url-pattern>/jsonservlet</url-pattern>
19+
</servlet-mapping>
20+
</web-app>

0 commit comments

Comments
 (0)