Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions gson-json-java/.gitignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
target
*.iml
.idea
3 changes: 3 additions & 0 deletions java-servlet-json/.gitignore
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
target
*.iml
.idea
1 change: 1 addition & 0 deletions java-servlet-json/README.md
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
http://hmkcode.com/java-servlet-send-receive-json-using-jquery-ajax/
1 change: 1 addition & 0 deletions java-servlet-json/modified.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
[{"title":"bbc","url":"news.bbc.co.uk","categories":["news"],"tags":["uk news"]},{"title":null,"url":"www.google.co.uk","categories":["youtube","gmail","gdrive"],"tags":["search"]}]
12 changes: 12 additions & 0 deletions java-servlet-json/pom.xml
Original file line numberDiff line numberDiff line change
Expand Up@@ -35,6 +35,18 @@
<artifactId>jackson-annotations</artifactId>
<version>2.2.2</version>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.9</version>
<exclusions>
<exclusion>
<artifactId>junit</artifactId>
<groupId>junit</groupId>
</exclusion>
</exclusions>
</dependency>

</dependencies>

<build>
Expand Down
28 changes: 14 additions & 14 deletions java-servlet-json/src/main/java/com/hmkcode/JSONServlet.java
Original file line numberDiff line numberDiff line change
@@ -1,48 +1,48 @@
package com.hmkcode;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.Article;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.List;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.Article;

public class JSONServlet extends HttpServlet{

private static final long serialVersionUID = 1L;

// This will store all received articles
List<Article> articles = new LinkedList<Article>();

/***************************************************
* URL: /jsonservlet
* doPost(): receives JSON data, parse it, map it and send back as JSON
****************************************************/
protected void doPost(HttpServletRequest request, HttpServletResponse response)
throws ServletException, IOException{

// 1. get received JSON data from request
BufferedReader br = new BufferedReader(new InputStreamReader(request.getInputStream()));
String json = ""
if(br != null){
json = br.readLine();
}

// 2. initiate jackson mapper
ObjectMapper mapper = new ObjectMapper();

// 3. Convert received JSON to Article
Article article = mapper.readValue(json, Article.class);

// 4. Set response type to JSON
response.setContentType("application/json");
response.setContentType("application/json");

// 5. Add article to List<Article>
articles.add(article);

Expand Down
33 changes: 4 additions & 29 deletions java-servlet-json/src/main/java/com/hmkcode/vo/Article.java
Original file line numberDiff line numberDiff line change
Expand Up@@ -5,35 +5,10 @@

public class Article{

private String title;
private String url;
private List<String> categories;
private List<String> tags;

public String getTitle(){
return title;
}
public void setTitle(String title){
this.title = title;
}
public String getUrl(){
return url;
}
public void setUrl(String url){
this.url = url;
}
public List<String> getCategories(){
return categories;
}
public void setCategories(List<String> categories){
this.categories = categories;
}
public List<String> getTags(){
return tags;
}
public void setTags(List<String> tags){
this.tags = tags;
}
public String title;
public String url;
public List<String> categories;
public List<String> tags;

public void addCategory(String category){
if(this.categories == null)
Expand Down
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
package com.hmkcode;/**
* Author: wge
* Date: 22/11/2013
* Time: 18:09
*/

import com.fasterxml.jackson.databind.ObjectMapper;
import com.hmkcode.vo.Article;
import org.junit.Test;

import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.util.ArrayList;
import java.util.List;


public class ArticleMapperTest
{
private static final java.util.logging.Logger log = java.util.logging.Logger.getLogger(ArticleMapperTest.class.getName());

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.



@Test
public void testValidJson() throws Exception

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
String json = readlineFromResourceFile("/valid.json");

ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(json, Article.class);

assert(article.url.equals("news.bbc.co.uk"));

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

assert(article.categories.contains("news"));
assert(article.categories.size() == 1);
assert(article.tags.size() == 1);
assert(article.tags.contains("uk news"));

List<Article> articles = new ArrayList<Article>();
articles.add(article);

Article extra = new Article();
extra.url = "www.google.co.uk"
List categoryList = new ArrayList<String>();
categoryList.add("youtube");
categoryList.add("gmail");
categoryList.add("gdrive");

extra.categories = categoryList;
extra.addTag("search");

articles.add(extra);

//this will appear in the root of your project.
mapper.writeValue(new File("modified.json"), articles);
}

@Test (expected=com.fasterxml.jackson.core.JsonParseException.class)
public void testBadInput() throws Exception
{
String json = readlineFromResourceFile("/malformed.json");
ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(json, Article.class);

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

}

@Test
public void missingUrl() throws Exception

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
String json = readlineFromResourceFile("/missingUrl.json");
ObjectMapper mapper = new ObjectMapper();
Article article = mapper.readValue(json, Article.class);
assert(article.url == null);
assert(article.categories.contains("news"));
assert(article.categories.size() == 1);
assert(article.tags.size() == 1);
assert(article.tags.contains("uk news"));


}

String readlineFromResourceFile(String filename) throws IOException

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

{
InputStream is = getClass().getResourceAsStream(filename);
byte[] buff = new byte[1000];
is.read(buff);
return new String(buff);
}
}
1 change: 1 addition & 0 deletions java-servlet-json/src/test/resources/malformed.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
{"title":"bbc","url":news.bbc.co.uk,"categories":["news"],"tags":["uk news"]};
1 change: 1 addition & 0 deletions java-servlet-json/src/test/resources/missingUrl.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
{"title":"bbc","categories":["news"],"tags":["uk news"]};
1 change: 1 addition & 0 deletions java-servlet-json/src/test/resources/valid.json
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
{"title":"bbc","url":"news.bbc.co.uk","categories":["news"],"tags":["uk news"]};