From ce7e3dc04a2a20bf69c8f13d841b4418a5da825e Mon Sep 17 00:00:00 2001 From: Pekka Helenius Date: Tue, 22 Sep 2020 23:59:29 +0300 Subject: [PATCH] Implement initial REST support Signed-off-by: Pekka Helenius --- .../com/fjordtek/bookstore/model/Book.java | 2 + .../fjordtek/bookstore/model/Category.java | 3 + .../bookstore/web/BookController.java | 4 ++ .../bookstore/web/BookRestController.java | 60 +++++++++++++++++++ .../src/main/resources/application.properties | 6 ++ .../src/main/resources/messages.properties | 5 ++ .../main/resources/templates/booklist.html | 38 ++++++++++-- 7 files changed, 112 insertions(+), 6 deletions(-) create mode 100644 bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java index df6485b..df6761e 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java @@ -24,6 +24,7 @@ import javax.validation.constraints.NotBlank; import javax.validation.constraints.Pattern; import javax.validation.constraints.Size; +import com.fasterxml.jackson.annotation.JsonManagedReference; import com.fjordtek.bookstore.annotation.CurrentYear; @Entity @@ -153,6 +154,7 @@ public class Book { ) private BigDecimal price; + @JsonManagedReference @ManyToOne( //fetch = FetchType.LAZY, optional = false diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java b/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java index ed7c0cb..93dac3a 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java @@ -11,6 +11,8 @@ import javax.persistence.Id; import javax.persistence.OneToMany; import javax.persistence.SequenceGenerator; +import com.fasterxml.jackson.annotation.JsonBackReference; + @Entity public class Category { @@ -31,6 +33,7 @@ public class Category { // Attributes with hard-coded constraints private String name; + @JsonBackReference @OneToMany( mappedBy = "category" //fetch = FetchType.LAZY, diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java index 7d1e37d..93933ae 100644 --- a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java @@ -46,6 +46,8 @@ public class BookController { @Autowired private CategoryRepository categoryRepository; + private static final String RestJSONPageView = "json"; + private static final String landingPageView = "index"; private static final String bookListPageView = "booklist"; private static final String bookAddPageView = "bookadd"; @@ -55,6 +57,8 @@ public class BookController { private Map globalModelMap = new HashMap() { private static final long serialVersionUID = 1L; { + put("restpage", RestJSONPageView); + put("indexpage", landingPageView); put("listpage", bookListPageView); put("addpage", bookAddPageView); diff --git a/bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java new file mode 100644 index 0000000..a8c7057 --- /dev/null +++ b/bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java @@ -0,0 +1,60 @@ +package com.fjordtek.bookstore.web; + +import java.util.Optional; + +import javax.servlet.http.HttpServletRequest; +import javax.servlet.http.HttpServletResponse; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.web.bind.annotation.PathVariable; +import org.springframework.web.bind.annotation.RequestMapping; +import org.springframework.web.bind.annotation.RequestMethod; +import org.springframework.web.bind.annotation.ResponseBody; +import org.springframework.web.bind.annotation.RestController; + +import com.fjordtek.bookstore.model.Book; +import com.fjordtek.bookstore.model.BookRepository; + +@RestController +@RequestMapping("json") +public class BookRestController { + + @Autowired + private BookRepository bookRepository; +/* + @Autowired + private CategoryRepository categoryRepository; +*/ + + private HttpServerLogger httpServerLogger = new HttpServerLogger(); + + @RequestMapping( + value = "booklist", + method = RequestMethod.GET + ) + public @ResponseBody Iterable getAllBooksRestData( + HttpServletRequest requestData, + HttpServletResponse responseData + ) { + + httpServerLogger.log(requestData, responseData); + + return bookRepository.findAll(); + } + + @RequestMapping( + value = "book" + "/{id}", + method = RequestMethod.GET + ) + public @ResponseBody Optional getBookRestData( + @PathVariable("id") Long bookId, + HttpServletRequest requestData, + HttpServletResponse responseData + ) { + + httpServerLogger.log(requestData, responseData); + + return bookRepository.findById(bookId); + } + +} \ No newline at end of file diff --git a/bookstore/src/main/resources/application.properties b/bookstore/src/main/resources/application.properties index f9b48f2..6fafbf7 100644 --- a/bookstore/src/main/resources/application.properties +++ b/bookstore/src/main/resources/application.properties @@ -12,6 +12,12 @@ spring.datasource.url=jdbc:h2:mem:testdb spring.jpa.show-sql=true +# REST API direct api URL ref +spring.data.rest.base-path=/api + + + + # Ref: https://docs.spring.io/spring-boot/docs/1.2.0.M2/reference/html/common-application-properties.html # # EMBEDDED SERVER CONFIGURATION (ServerProperties) diff --git a/bookstore/src/main/resources/messages.properties b/bookstore/src/main/resources/messages.properties index 8ca6b61..0513569 100644 --- a/bookstore/src/main/resources/messages.properties +++ b/bookstore/src/main/resources/messages.properties @@ -12,6 +12,7 @@ book.isbn = ISBN book.year = Year book.price = Price book.category = Category +book.json = JSON Data book.desc.set.author = Set book author name book.desc.set.title = Set book primary title @@ -32,6 +33,8 @@ book.error.title = Invalid title book.error.isbn = Invalid ISBN code book.error.year = Invalid year book.error.price = Invalid price +book.error.isbn.exists = ISBN code already exists +book.error.id.invalid = Wrong book page.title.browser.list = Bookstore page page.title.browser.add = Add book @@ -46,6 +49,7 @@ page.title.webform.error = Error page.text.list.actions = Actions page.text.list.delete = Delete page.text.list.edit = Edit +page.text.list.json = Get page.text.error = Error while processing your request @@ -53,5 +57,6 @@ button.book.add = Add book button.book.edit = Update book button.page.list.return = Return to book list page +button.page.list.json = Get list as JSON page.symbols.currency = \u20AC \ No newline at end of file diff --git a/bookstore/src/main/resources/templates/booklist.html b/bookstore/src/main/resources/templates/booklist.html index a9a67a8..154cb7c 100644 --- a/bookstore/src/main/resources/templates/booklist.html +++ b/bookstore/src/main/resources/templates/booklist.html @@ -29,6 +29,7 @@ book.price page.text.list.actions + book.json @@ -57,15 +58,40 @@ page.text.list.edit + + + + page.text.list.json + + + + + + + + button.book.add + + + + + + + + + + + + button.page.list.json + + + -