Browse Source

Implement initial REST support

Signed-off-by: Pekka Helenius <fincer89@hotmail.com>
v0.0.2-alpha
Pekka Helenius 4 years ago
parent
commit
ce7e3dc04a
7 changed files with 112 additions and 6 deletions
  1. +2
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java
  2. +3
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java
  3. +4
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java
  4. +60
    -0
      bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java
  5. +6
    -0
      bookstore/src/main/resources/application.properties
  6. +5
    -0
      bookstore/src/main/resources/messages.properties
  7. +32
    -6
      bookstore/src/main/resources/templates/booklist.html

+ 2
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/Book.java View File

@ -24,6 +24,7 @@ import javax.validation.constraints.NotBlank;
import javax.validation.constraints.Pattern; import javax.validation.constraints.Pattern;
import javax.validation.constraints.Size; import javax.validation.constraints.Size;
import com.fasterxml.jackson.annotation.JsonManagedReference;
import com.fjordtek.bookstore.annotation.CurrentYear; import com.fjordtek.bookstore.annotation.CurrentYear;
@Entity @Entity
@ -153,6 +154,7 @@ public class Book {
) )
private BigDecimal price; private BigDecimal price;
@JsonManagedReference
@ManyToOne( @ManyToOne(
//fetch = FetchType.LAZY, //fetch = FetchType.LAZY,
optional = false optional = false


+ 3
- 0
bookstore/src/main/java/com/fjordtek/bookstore/model/Category.java View File

@ -11,6 +11,8 @@ import javax.persistence.Id;
import javax.persistence.OneToMany; import javax.persistence.OneToMany;
import javax.persistence.SequenceGenerator; import javax.persistence.SequenceGenerator;
import com.fasterxml.jackson.annotation.JsonBackReference;
@Entity @Entity
public class Category { public class Category {
@ -31,6 +33,7 @@ public class Category {
// Attributes with hard-coded constraints // Attributes with hard-coded constraints
private String name; private String name;
@JsonBackReference
@OneToMany( @OneToMany(
mappedBy = "category" mappedBy = "category"
//fetch = FetchType.LAZY, //fetch = FetchType.LAZY,


+ 4
- 0
bookstore/src/main/java/com/fjordtek/bookstore/web/BookController.java View File

@ -46,6 +46,8 @@ public class BookController {
@Autowired @Autowired
private CategoryRepository categoryRepository; private CategoryRepository categoryRepository;
private static final String RestJSONPageView = "json";
private static final String landingPageView = "index"; private static final String landingPageView = "index";
private static final String bookListPageView = "booklist"; private static final String bookListPageView = "booklist";
private static final String bookAddPageView = "bookadd"; private static final String bookAddPageView = "bookadd";
@ -55,6 +57,8 @@ public class BookController {
private Map<String,String> globalModelMap = new HashMap<String,String>() { private Map<String,String> globalModelMap = new HashMap<String,String>() {
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
{ {
put("restpage", RestJSONPageView);
put("indexpage", landingPageView); put("indexpage", landingPageView);
put("listpage", bookListPageView); put("listpage", bookListPageView);
put("addpage", bookAddPageView); put("addpage", bookAddPageView);


+ 60
- 0
bookstore/src/main/java/com/fjordtek/bookstore/web/BookRestController.java View File

@ -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<Book> getAllBooksRestData(
HttpServletRequest requestData,
HttpServletResponse responseData
) {
httpServerLogger.log(requestData, responseData);
return bookRepository.findAll();
}
@RequestMapping(
value = "book" + "/{id}",
method = RequestMethod.GET
)
public @ResponseBody Optional<Book> getBookRestData(
@PathVariable("id") Long bookId,
HttpServletRequest requestData,
HttpServletResponse responseData
) {
httpServerLogger.log(requestData, responseData);
return bookRepository.findById(bookId);
}
}

+ 6
- 0
bookstore/src/main/resources/application.properties View File

@ -12,6 +12,12 @@ spring.datasource.url=jdbc:h2:mem:testdb
spring.jpa.show-sql=true 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 # Ref: https://docs.spring.io/spring-boot/docs/1.2.0.M2/reference/html/common-application-properties.html
# # EMBEDDED SERVER CONFIGURATION (ServerProperties) # # EMBEDDED SERVER CONFIGURATION (ServerProperties)


+ 5
- 0
bookstore/src/main/resources/messages.properties View File

@ -12,6 +12,7 @@ book.isbn = ISBN
book.year = Year book.year = Year
book.price = Price book.price = Price
book.category = Category book.category = Category
book.json = JSON Data
book.desc.set.author = Set book author name book.desc.set.author = Set book author name
book.desc.set.title = Set book primary title 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.isbn = Invalid ISBN code
book.error.year = Invalid year book.error.year = Invalid year
book.error.price = Invalid price 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.list = Bookstore page
page.title.browser.add = Add book page.title.browser.add = Add book
@ -46,6 +49,7 @@ page.title.webform.error = Error
page.text.list.actions = Actions page.text.list.actions = Actions
page.text.list.delete = Delete page.text.list.delete = Delete
page.text.list.edit = Edit page.text.list.edit = Edit
page.text.list.json = Get
page.text.error = Error while processing your request page.text.error = Error while processing your request
@ -53,5 +57,6 @@ button.book.add = Add book
button.book.edit = Update book button.book.edit = Update book
button.page.list.return = Return to book list page button.page.list.return = Return to book list page
button.page.list.json = Get list as JSON
page.symbols.currency = \u20AC page.symbols.currency = \u20AC

+ 32
- 6
bookstore/src/main/resources/templates/booklist.html View File

@ -29,6 +29,7 @@
<th th:text="${#messages.msgOrNull('book.price')} ?: 'book.price'">book.price</th> <th th:text="${#messages.msgOrNull('book.price')} ?: 'book.price'">book.price</th>
<th th:text="${#messages.msgOrNull('page.text.list.actions')} ?: 'page.text.list.actions'">page.text.list.actions</th> <th th:text="${#messages.msgOrNull('page.text.list.actions')} ?: 'page.text.list.actions'">page.text.list.actions</th>
<th></th> <th></th>
<th th:text="${#messages.msgOrNull('book.json')} ?: 'book.json'">book.json</th>
</tr> </tr>
<tr th:each="book : ${books}"> <tr th:each="book : ${books}">
@ -57,15 +58,40 @@
page.text.list.edit page.text.list.edit
</a> </a>
</td> </td>
<td>
<a class="btn btn-info"
th:href="@{__${restpage}__/book/{id}(id=${book.id})}"
th:text="${#messages.msgOrNull('page.text.list.json')} ?: 'page.text.list.json'">
page.text.list.json
</a>
</td>
</tr>
<tr>
<td>
<a class="btn btn-success" th:href="@{__${addpage}__}"
th:text="${#messages.msgOrNull('button.book.add')} ?: 'button.book.add'">
button.book.add
</a>
</td>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<th></th>
<td>
<a class="btn btn-dark" th:href="@{__${restpage}__} + '/' + @{__${listpage}__}"
th:text="${#messages.msgOrNull('button.page.list.json')} ?: 'button.page.list.json'">
button.page.list.json
</a>
</td>
</tr> </tr>
</table> </table>
<div>
<a class="btn btn-success" th:href="@{__${addpage}__}"
th:text="${#messages.msgOrNull('button.book.add')} ?: 'button.book.add'">
button.book.add
</a>
</div>
</div> </div>
</body> </body>
</html> </html>

Loading…
Cancel
Save