rest with spark java (#1028)

This commit is contained in:
Abhinab Kanrar 2017-01-22 18:25:28 +05:30 committed by Grzegorz Piwowarek
parent f7236b301b
commit 7ba5cfb314
5 changed files with 328 additions and 0 deletions

View File

@ -0,0 +1,38 @@
<?xml version="1.0"?>
<project xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd" xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>com.baeldung</groupId>
<artifactId>parent-modules</artifactId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>com.baeldung</groupId>
<artifactId>rest-with-spark-java</artifactId>
<version>1.0-SNAPSHOT</version>
<name>rest-with-spark-java</name>
<url>http://maven.apache.org</url>
<dependencies>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>3.8.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.sparkjava</groupId>
<artifactId>spark-core</artifactId>
<version>2.5.4</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-core</artifactId>
<version>2.8.6</version>
</dependency>
<dependency>
<groupId>com.fasterxml.jackson.core</groupId>
<artifactId>jackson-databind</artifactId>
<version>2.8.6</version>
</dependency>
</dependencies>
</project>

View File

@ -0,0 +1,50 @@
package com.baeldung;
import static spark.Spark.after;
import static spark.Spark.before;
import static spark.Spark.delete;
import static spark.Spark.get;
import static spark.Spark.post;
import static spark.Spark.port;
import com.baeldung.domain.Book;
import com.baeldung.service.LibraryService;
public class Router {
public static void main( String[] args ){
port(8080);
before((request, response) -> {
//do some filtering stuff
});
after((request, response) -> {
response.type("application/json");
});
get("ListOfBooks", (request, response) -> {
return LibraryService.view();
});
get("SearchBook/:title", (request, response) -> {
return LibraryService.view(request.params("title"));
});
post("AddBook/:title/:author/:publisher", (request, response) -> {
Book book = new Book();
book.setTitle(request.params("title"));
book.setAuthor(request.params("author"));
book.setPublisher(request.params("publisher"));
return LibraryService.add(book);
});
delete("DeleteBook/:title", (request, response) -> {
return LibraryService.delete(request.params("title"));
});
}
}

View File

@ -0,0 +1,52 @@
package com.baeldung.domain;
public class Book {
private String title;
private String author;
private String publisher;
public Book() {}
public Book(String title, String author, String publisher) {
super();
this.title = title;
this.author = author;
this.publisher = publisher;
}
public String getTitle() {
return title;
}
public void setTitle(String title) {
this.title = title;
}
public String getAuthor() {
return author;
}
public void setAuthor(String author) {
this.author = author;
}
public String getPublisher() {
return publisher;
}
public void setPublisher(String publisher) {
this.publisher = publisher;
}
protected boolean canEqual(Object other) {
return other instanceof Book;
}
@Override
public boolean equals(Object o) {
if (o == this) return true;
if (!(o instanceof Book)) return false;
Book other = (Book) o;
if (!other.canEqual((Object)this)) return false;
if (this.getTitle() == null ? other.getTitle() != null : !this.getTitle().equals(other.getTitle())) return false;
return true;
}
}

View File

@ -0,0 +1,40 @@
package com.baeldung.service;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.baeldung.domain.Book;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.ObjectWriter;
public class LibraryService {
private static ObjectWriter mapper = new ObjectMapper().writer().withDefaultPrettyPrinter();
private static Map<String, Book> library = new HashMap<String,Book>();
public static String view() throws JsonProcessingException {
List<Book> books = new ArrayList<Book>();
library.forEach((key, value) -> {
books.add(value);
});
return mapper.writeValueAsString(books);
}
public static String view(String title) throws JsonProcessingException {
return mapper.writeValueAsString(library.get(title));
}
public static String add(Book book) throws JsonProcessingException {
library.put(book.getTitle(), book);
return mapper.writeValueAsString(book);
}
public static String delete(String title) throws JsonProcessingException {
Book deletedBook = library.remove(title);
return mapper.writeValueAsString(deletedBook);
}
}

View File

@ -0,0 +1,148 @@
package com.baeldung;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.List;
import com.baeldung.domain.Book;
import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import junit.framework.Test;
import junit.framework.TestCase;
import junit.framework.TestSuite;
public class AppTest extends TestCase {
ObjectMapper mapper = new ObjectMapper();
public AppTest( String testName ) {
super( testName );
}
public static Test suite() {
return new TestSuite( AppTest.class );
}
public void testApp() throws IOException, ClassNotFoundException {
URL url;
HttpURLConnection conn;
BufferedReader br;
String output;
StringBuffer resp;
Book book;
Book temp;
url = new URL("http://localhost:8080/AddBook/Odessy/YannMartel/GreenLeaves");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
conn.getContent();
br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
resp = new StringBuffer();
while ((output = br.readLine()) != null) {
resp.append(output);
}
book = mapper.readValue(resp.toString(), Book.class);
temp = new Book("Odessy","YannMartel","GreenLeaves");
assertEquals(book, temp);
url = new URL("http://localhost:8080/AddBook/Twilight/StephenieMeyer/LittleBrown");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("POST");
br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
resp = new StringBuffer();
while ((output = br.readLine()) != null) {
resp.append(output);
}
book = mapper.readValue(resp.toString(), Book.class);
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
assertEquals(book, temp);
url = new URL("http://localhost:8080/ListOfBooks");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
resp = new StringBuffer();
while ((output = br.readLine()) != null) {
resp.append(output);
}
List<Book> books = new ArrayList<Book>();
books.add(new Book("Odessy","YannMartel","GreenLeaves"));
books.add(new Book("Twilight","StephenieMeyer","LittleBrown"));
List<Book> listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){});
assertEquals(books, listOfBooks);
url = new URL("http://localhost:8080/SearchBook/Twilight");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
resp = new StringBuffer();
while ((output = br.readLine()) != null) {
resp.append(output);
}
book = mapper.readValue(resp.toString(), Book.class);
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
assertEquals(book, temp);
url = new URL("http://localhost:8080/DeleteBook/Twilight");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("DELETE");
br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
resp = new StringBuffer();
while ((output = br.readLine()) != null) {
resp.append(output);
}
book = mapper.readValue(resp.toString(), Book.class);
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
assertEquals(book, temp);
url = new URL("http://localhost:8080/ListOfBooks");
conn = (HttpURLConnection) url.openConnection();
conn.setRequestMethod("GET");
br = new BufferedReader(new InputStreamReader(
(conn.getInputStream())));
resp = new StringBuffer();
while ((output = br.readLine()) != null) {
resp.append(output);
}
books = new ArrayList<Book>();
books.add(new Book("Odessy","YannMartel","GreenLeaves"));
listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){});
assertEquals(books, listOfBooks);
conn.disconnect();
}
}