rest with spark java (#1028)
This commit is contained in:
committed by
Grzegorz Piwowarek
parent
f7236b301b
commit
7ba5cfb314
50
rest-with-spark-java/src/main/java/com/baeldung/Router.java
Normal file
50
rest-with-spark-java/src/main/java/com/baeldung/Router.java
Normal 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"));
|
||||
});
|
||||
|
||||
}
|
||||
}
|
||||
@@ -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;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -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);
|
||||
}
|
||||
|
||||
}
|
||||
Reference in New Issue
Block a user