diff --git a/rest-with-spark-java/pom.xml b/rest-with-spark-java/pom.xml new file mode 100644 index 0000000000..2364154fdc --- /dev/null +++ b/rest-with-spark-java/pom.xml @@ -0,0 +1,38 @@ + + + 4.0.0 + + com.baeldung + parent-modules + 1.0.0-SNAPSHOT + + com.baeldung + rest-with-spark-java + 1.0-SNAPSHOT + rest-with-spark-java + http://maven.apache.org + + + junit + junit + 3.8.1 + test + + + com.sparkjava + spark-core + 2.5.4 + + + com.fasterxml.jackson.core + jackson-core + 2.8.6 + + + com.fasterxml.jackson.core + jackson-databind + 2.8.6 + + + diff --git a/rest-with-spark-java/src/main/java/com/baeldung/Router.java b/rest-with-spark-java/src/main/java/com/baeldung/Router.java new file mode 100644 index 0000000000..3482184e1e --- /dev/null +++ b/rest-with-spark-java/src/main/java/com/baeldung/Router.java @@ -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")); + }); + + } +} diff --git a/rest-with-spark-java/src/main/java/com/baeldung/domain/Book.java b/rest-with-spark-java/src/main/java/com/baeldung/domain/Book.java new file mode 100644 index 0000000000..977d5d9f89 --- /dev/null +++ b/rest-with-spark-java/src/main/java/com/baeldung/domain/Book.java @@ -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; + } + +} diff --git a/rest-with-spark-java/src/main/java/com/baeldung/service/LibraryService.java b/rest-with-spark-java/src/main/java/com/baeldung/service/LibraryService.java new file mode 100644 index 0000000000..e4ca4c270c --- /dev/null +++ b/rest-with-spark-java/src/main/java/com/baeldung/service/LibraryService.java @@ -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 library = new HashMap(); + + public static String view() throws JsonProcessingException { + List books = new ArrayList(); + 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); + } + +} diff --git a/rest-with-spark-java/src/test/java/com/baeldung/AppTest.java b/rest-with-spark-java/src/test/java/com/baeldung/AppTest.java new file mode 100644 index 0000000000..c7b88dd6d1 --- /dev/null +++ b/rest-with-spark-java/src/test/java/com/baeldung/AppTest.java @@ -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 books = new ArrayList(); + + books.add(new Book("Odessy","YannMartel","GreenLeaves")); + books.add(new Book("Twilight","StephenieMeyer","LittleBrown")); + + List listOfBooks = mapper.readValue(resp.toString(), new TypeReference>(){}); + + 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(); + + books.add(new Book("Odessy","YannMartel","GreenLeaves")); + listOfBooks = mapper.readValue(resp.toString(), new TypeReference>(){}); + + assertEquals(books, listOfBooks); + + conn.disconnect(); + + } + +}