Skip to content

Commit 7ba5cfb

Browse files
AbhinabKanrarpivovarit
authored andcommitted
rest with spark java (eugenp#1028)
1 parent f7236b3 commit 7ba5cfb

5 files changed

Lines changed: 328 additions & 0 deletions

File tree

‎rest-with-spark-java/pom.xml‎

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?xml version="1.0"?>
2+
<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"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance">
4+
<modelVersion>4.0.0</modelVersion>
5+
<parent>
6+
<groupId>com.baeldung</groupId>
7+
<artifactId>parent-modules</artifactId>
8+
<version>1.0.0-SNAPSHOT</version>
9+
</parent>
10+
<groupId>com.baeldung</groupId>
11+
<artifactId>rest-with-spark-java</artifactId>
12+
<version>1.0-SNAPSHOT</version>
13+
<name>rest-with-spark-java</name>
14+
<url>http://maven.apache.org</url>
15+
<dependencies>
16+
<dependency>
17+
<groupId>junit</groupId>
18+
<artifactId>junit</artifactId>
19+
<version>3.8.1</version>
20+
<scope>test</scope>
21+
</dependency>
22+
<dependency>
23+
<groupId>com.sparkjava</groupId>
24+
<artifactId>spark-core</artifactId>
25+
<version>2.5.4</version>
26+
</dependency>
27+
<dependency>
28+
<groupId>com.fasterxml.jackson.core</groupId>
29+
<artifactId>jackson-core</artifactId>
30+
<version>2.8.6</version>
31+
</dependency>
32+
<dependency>
33+
<groupId>com.fasterxml.jackson.core</groupId>
34+
<artifactId>jackson-databind</artifactId>
35+
<version>2.8.6</version>
36+
</dependency>
37+
</dependencies>
38+
</project>
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package com.baeldung;
2+
3+
import static spark.Spark.after;
4+
import static spark.Spark.before;
5+
import static spark.Spark.delete;
6+
import static spark.Spark.get;
7+
import static spark.Spark.post;
8+
import static spark.Spark.port;
9+
10+
import com.baeldung.domain.Book;
11+
import com.baeldung.service.LibraryService;
12+
13+
public class Router {
14+
15+
public static void main( String[] args ){
16+
17+
port(8080);
18+
19+
before((request, response) -> {
20+
21+
//do some filtering stuff
22+
23+
});
24+
25+
after((request, response) -> {
26+
response.type("application/json");
27+
});
28+
29+
get("ListOfBooks", (request, response) -> {
30+
return LibraryService.view();
31+
});
32+
33+
get("SearchBook/:title", (request, response) -> {
34+
return LibraryService.view(request.params("title"));
35+
});
36+
37+
post("AddBook/:title/:author/:publisher", (request, response) -> {
38+
Book book = new Book();
39+
book.setTitle(request.params("title"));
40+
book.setAuthor(request.params("author"));
41+
book.setPublisher(request.params("publisher"));
42+
return LibraryService.add(book);
43+
});
44+
45+
delete("DeleteBook/:title", (request, response) -> {
46+
return LibraryService.delete(request.params("title"));
47+
});
48+
49+
}
50+
}
Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package com.baeldung.domain;
2+
3+
public class Book {
4+
5+
private String title;
6+
private String author;
7+
private String publisher;
8+
9+
public Book() {}
10+
11+
public Book(String title, String author, String publisher) {
12+
super();
13+
this.title = title;
14+
this.author = author;
15+
this.publisher = publisher;
16+
}
17+
18+
public String getTitle() {
19+
return title;
20+
}
21+
public void setTitle(String title) {
22+
this.title = title;
23+
}
24+
public String getAuthor() {
25+
return author;
26+
}
27+
28+
public void setAuthor(String author) {
29+
this.author = author;
30+
}
31+
public String getPublisher() {
32+
return publisher;
33+
}
34+
public void setPublisher(String publisher) {
35+
this.publisher = publisher;
36+
}
37+
38+
protected boolean canEqual(Object other) {
39+
return other instanceof Book;
40+
}
41+
42+
@Override
43+
public boolean equals(Object o) {
44+
if (o == this) return true;
45+
if (!(o instanceof Book)) return false;
46+
Book other = (Book) o;
47+
if (!other.canEqual((Object)this)) return false;
48+
if (this.getTitle() == null ? other.getTitle() != null : !this.getTitle().equals(other.getTitle())) return false;
49+
return true;
50+
}
51+
52+
}
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
package com.baeldung.service;
2+
3+
import java.util.ArrayList;
4+
import java.util.HashMap;
5+
import java.util.List;
6+
import java.util.Map;
7+
8+
import com.baeldung.domain.Book;
9+
import com.fasterxml.jackson.core.JsonProcessingException;
10+
import com.fasterxml.jackson.databind.ObjectMapper;
11+
import com.fasterxml.jackson.databind.ObjectWriter;
12+
13+
public class LibraryService {
14+
15+
private static ObjectWriter mapper = new ObjectMapper().writer().withDefaultPrettyPrinter();
16+
private static Map<String, Book> library = new HashMap<String,Book>();
17+
18+
public static String view() throws JsonProcessingException {
19+
List<Book> books = new ArrayList<Book>();
20+
library.forEach((key, value) -> {
21+
books.add(value);
22+
});
23+
return mapper.writeValueAsString(books);
24+
}
25+
26+
public static String view(String title) throws JsonProcessingException {
27+
return mapper.writeValueAsString(library.get(title));
28+
}
29+
30+
public static String add(Book book) throws JsonProcessingException {
31+
library.put(book.getTitle(), book);
32+
return mapper.writeValueAsString(book);
33+
}
34+
35+
public static String delete(String title) throws JsonProcessingException {
36+
Book deletedBook = library.remove(title);
37+
return mapper.writeValueAsString(deletedBook);
38+
}
39+
40+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
1+
package com.baeldung;
2+
3+
import java.io.BufferedReader;
4+
import java.io.IOException;
5+
import java.io.InputStreamReader;
6+
import java.net.HttpURLConnection;
7+
import java.net.URL;
8+
import java.util.ArrayList;
9+
import java.util.List;
10+
11+
import com.baeldung.domain.Book;
12+
import com.fasterxml.jackson.core.type.TypeReference;
13+
import com.fasterxml.jackson.databind.ObjectMapper;
14+
15+
import junit.framework.Test;
16+
import junit.framework.TestCase;
17+
import junit.framework.TestSuite;
18+
19+
public class AppTest extends TestCase {
20+
21+
ObjectMapper mapper = new ObjectMapper();
22+
23+
public AppTest( String testName ) {
24+
super( testName );
25+
}
26+
27+
public static Test suite() {
28+
return new TestSuite( AppTest.class );
29+
}
30+
31+
public void testApp() throws IOException, ClassNotFoundException {
32+
33+
URL url;
34+
HttpURLConnection conn;
35+
BufferedReader br;
36+
String output;
37+
StringBuffer resp;
38+
Book book;
39+
Book temp;
40+
41+
url = new URL("http://localhost:8080/AddBook/Odessy/YannMartel/GreenLeaves");
42+
conn = (HttpURLConnection) url.openConnection();
43+
conn.setRequestMethod("POST");
44+
conn.getContent();
45+
br = new BufferedReader(new InputStreamReader(
46+
(conn.getInputStream())));
47+
resp = new StringBuffer();
48+
49+
while ((output = br.readLine()) != null) {
50+
resp.append(output);
51+
}
52+
53+
book = mapper.readValue(resp.toString(), Book.class);
54+
temp = new Book("Odessy","YannMartel","GreenLeaves");
55+
56+
assertEquals(book, temp);
57+
58+
url = new URL("http://localhost:8080/AddBook/Twilight/StephenieMeyer/LittleBrown");
59+
conn = (HttpURLConnection) url.openConnection();
60+
conn.setRequestMethod("POST");
61+
br = new BufferedReader(new InputStreamReader(
62+
(conn.getInputStream())));
63+
resp = new StringBuffer();
64+
65+
while ((output = br.readLine()) != null) {
66+
resp.append(output);
67+
}
68+
69+
book = mapper.readValue(resp.toString(), Book.class);
70+
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
71+
72+
assertEquals(book, temp);
73+
74+
url = new URL("http://localhost:8080/ListOfBooks");
75+
conn = (HttpURLConnection) url.openConnection();
76+
conn.setRequestMethod("GET");
77+
br = new BufferedReader(new InputStreamReader(
78+
(conn.getInputStream())));
79+
resp = new StringBuffer();
80+
81+
while ((output = br.readLine()) != null) {
82+
resp.append(output);
83+
}
84+
85+
List<Book> books = new ArrayList<Book>();
86+
87+
books.add(new Book("Odessy","YannMartel","GreenLeaves"));
88+
books.add(new Book("Twilight","StephenieMeyer","LittleBrown"));
89+
90+
List<Book> listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){});
91+
92+
assertEquals(books, listOfBooks);
93+
94+
url = new URL("http://localhost:8080/SearchBook/Twilight");
95+
conn = (HttpURLConnection) url.openConnection();
96+
conn.setRequestMethod("GET");
97+
br = new BufferedReader(new InputStreamReader(
98+
(conn.getInputStream())));
99+
resp = new StringBuffer();
100+
101+
while ((output = br.readLine()) != null) {
102+
resp.append(output);
103+
}
104+
105+
book = mapper.readValue(resp.toString(), Book.class);
106+
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
107+
108+
assertEquals(book, temp);
109+
110+
url = new URL("http://localhost:8080/DeleteBook/Twilight");
111+
conn = (HttpURLConnection) url.openConnection();
112+
conn.setRequestMethod("DELETE");
113+
br = new BufferedReader(new InputStreamReader(
114+
(conn.getInputStream())));
115+
resp = new StringBuffer();
116+
117+
while ((output = br.readLine()) != null) {
118+
resp.append(output);
119+
}
120+
121+
book = mapper.readValue(resp.toString(), Book.class);
122+
temp = new Book("Twilight","StephenieMeyer","LittleBrown");
123+
124+
assertEquals(book, temp);
125+
126+
url = new URL("http://localhost:8080/ListOfBooks");
127+
conn = (HttpURLConnection) url.openConnection();
128+
conn.setRequestMethod("GET");
129+
br = new BufferedReader(new InputStreamReader(
130+
(conn.getInputStream())));
131+
resp = new StringBuffer();
132+
133+
while ((output = br.readLine()) != null) {
134+
resp.append(output);
135+
}
136+
137+
books = new ArrayList<Book>();
138+
139+
books.add(new Book("Odessy","YannMartel","GreenLeaves"));
140+
listOfBooks = mapper.readValue(resp.toString(), new TypeReference<List<Book>>(){});
141+
142+
assertEquals(books, listOfBooks);
143+
144+
conn.disconnect();
145+
146+
}
147+
148+
}

0 commit comments

Comments
 (0)