├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── mightyjava
│ │ ├── Application.java
│ │ ├── config
│ │ ├── JSONPrettyPrintConfig.java
│ │ └── MyCommandLineRunner.java
│ │ ├── data
│ │ └── BookData.java
│ │ ├── domain
│ │ └── Book.java
│ │ ├── exception
│ │ ├── ApplicationException.java
│ │ ├── BookCustomExceptionHandler.java
│ │ ├── BookExceptionHandler.java
│ │ └── BookNotFoundException.java
│ │ ├── filter
│ │ ├── ActuatorFilter.java
│ │ ├── BookFilter.java
│ │ ├── BookFilterTwo.java
│ │ └── FilterConfig.java
│ │ ├── interceptor
│ │ ├── BookInterceptor.java
│ │ └── BookInterceptorConfig.java
│ │ ├── repository
│ │ └── BookRepository.java
│ │ ├── resource
│ │ ├── Resource.java
│ │ └── impl
│ │ │ └── BookResourceImpl.java
│ │ ├── service
│ │ ├── IService.java
│ │ └── impl
│ │ │ └── BookServiceImpl.java
│ │ └── util
│ │ └── MethodUtils.java
└── resources
│ ├── application.properties
│ ├── data
│ └── books.json
│ └── qrcodes
│ └── QRCode.png
└── test
└── java
└── com
└── mightyjava
└── ApplicationTests.java
/README.md:
--------------------------------------------------------------------------------
1 | # GET
2 | http://localhost:8081/rest/books
3 | # GET By ID
4 | http://localhost:8081/rest/books/cff6c1c3-be92-4cb3-9c79-5a67f63a3d61
5 | # POST
6 | http://localhost:8081/rest/books
7 | # PUT
8 | http://localhost:8081/rest/books
9 | # PATCH
10 | http://localhost:8081/rest/books/cff6c1c3-be92-4cb3-9c79-5a67f63a3d61
11 | # DELETE
12 | http://localhost:8081/rest/books/cff6c1c3-be92-4cb3-9c79-5a67f63a3d61
13 | # Image QR Code
14 | http://localhost:8081/rest/books/generateImageQRCode/{bookId}
15 | # Byte QR Code
16 | http://localhost:8081/rest/books/generateByteQRCode/{bookId}
17 |
18 |
19 |
20 | # After Spring Data Rest
21 | # Book
22 | http://localhost:8081/rest/book
23 | # Search
24 | http://localhost:8081/rest/book/search
25 | # Find By Title
26 | http://localhost:8081/rest/book/search/findByTitle?title=Spring%20Microservices%20in%20Action
27 | # Find By Author
28 | http://localhost:8081/rest/book/search/findByAuthor?author=John%20Carnell
29 | # Find By ISBN Number
30 | http://localhost:8081/rest/book/search/findByIsbnNumber?isbnNumber=9351199193
31 | # Find By Language
32 | http://localhost:8081/rest/book/search/findByLanguage?language=English
33 | # Find By Price
34 | http://localhost:8081/rest/book/search/findByPrice?price=2776
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | org.springframework.boot
8 | spring-boot-starter-parent
9 | 2.4.3
10 |
11 |
12 | com.mightyjava
13 | book-rest-api
14 | 0.0.1-SNAPSHOT
15 | war
16 | book-rest-api
17 | Demo project for Spring Boot
18 |
19 |
20 | 1.8
21 |
22 |
23 |
24 |
25 | org.springframework.boot
26 | spring-boot-starter-web
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-actuator
32 |
33 |
34 |
35 | org.springframework.boot
36 | spring-boot-devtools
37 | runtime
38 |
39 |
40 |
41 | org.springframework.boot
42 | spring-boot-starter-data-jpa
43 |
44 |
45 |
46 | com.h2database
47 | h2
48 | runtime
49 |
50 |
51 |
52 | org.springframework.boot
53 | spring-boot-starter-test
54 | test
55 |
56 |
57 |
58 | com.sun.jersey
59 | jersey-json
60 | 1.4
61 |
62 |
63 |
64 | com.sun.jersey
65 | jersey-server
66 | 1.4
67 |
68 |
69 |
70 | org.projectlombok
71 | lombok
72 | provided
73 |
74 |
75 |
76 | org.springframework.boot
77 | spring-boot-starter-hateoas
78 |
79 |
80 |
81 | org.springframework.boot
82 | spring-boot-starter-data-rest
83 |
84 |
85 |
86 | com.google.zxing
87 | core
88 | 3.4.1
89 |
90 |
91 |
92 | com.google.zxing
93 | javase
94 | 3.4.1
95 |
96 |
97 |
98 |
99 |
100 |
101 | org.springframework.boot
102 | spring-boot-maven-plugin
103 |
104 |
105 |
106 | org.projectlombok
107 | lombok
108 |
109 |
110 |
111 |
112 |
113 |
114 |
115 |
116 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/Application.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava;
2 |
3 | import org.springframework.boot.SpringApplication;
4 | import org.springframework.boot.autoconfigure.SpringBootApplication;
5 |
6 | @SpringBootApplication
7 | public class Application {
8 |
9 | public static void main(String[] args) {
10 | SpringApplication.run(Application.class, args);
11 | }
12 |
13 | }
14 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/config/JSONPrettyPrintConfig.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava.config;
2 |
3 | import java.util.List;
4 |
5 | import org.springframework.context.annotation.Configuration;
6 | import org.springframework.http.converter.HttpMessageConverter;
7 | import org.springframework.http.converter.json.MappingJackson2HttpMessageConverter;
8 | import org.springframework.web.servlet.config.annotation.WebMvcConfigurationSupport;
9 |
10 | @Configuration
11 | public class JSONPrettyPrintConfig extends WebMvcConfigurationSupport {
12 |
13 | @Override
14 | protected void extendMessageConverters(List> converters) {
15 | converters.forEach(converter -> {
16 | if (converter instanceof MappingJackson2HttpMessageConverter) {
17 | MappingJackson2HttpMessageConverter jsonConverter = (MappingJackson2HttpMessageConverter) converter;
18 | jsonConverter.setPrettyPrint(true);
19 | }
20 | });
21 | }
22 | }
23 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/config/MyCommandLineRunner.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava.config;
2 |
3 | import java.io.InputStream;
4 | import java.util.ArrayList;
5 | import java.util.List;
6 |
7 | import org.springframework.beans.factory.annotation.Autowired;
8 | import org.springframework.boot.CommandLineRunner;
9 | import org.springframework.context.annotation.Configuration;
10 |
11 | import com.fasterxml.jackson.core.type.TypeReference;
12 | import com.fasterxml.jackson.databind.ObjectMapper;
13 | import com.mightyjava.data.BookData;
14 | import com.mightyjava.domain.Book;
15 | import com.mightyjava.service.IService;
16 |
17 | @Configuration
18 | public class MyCommandLineRunner implements CommandLineRunner {
19 |
20 | private final String BOOKS_JSON = "/data/books.json";
21 |
22 | @Autowired
23 | private IService service;
24 |
25 | @Override
26 | public void run(String... args) throws Exception {
27 | if (service.findAll().size() == 0) {
28 | try {
29 | TypeReference> typeReference = new TypeReference>() {
30 | };
31 | InputStream inputStream = TypeReference.class.getResourceAsStream(BOOKS_JSON);
32 | List books = new ObjectMapper().readValue(inputStream, typeReference);
33 | if (books != null && !books.isEmpty()) {
34 | List bookList = new ArrayList<>();
35 | books.forEach(book -> bookList.add(new Book(book.getTitle(), book.getAuthor(),
36 | book.getCoverPhotoURL(), book.getIsbnNumber(), book.getPrice(), book.getLanguage())));
37 | List savedBookList = service.saveAll(bookList);
38 | System.out.println(savedBookList.size());
39 | }
40 | } catch (Exception ex) {
41 | System.out.println("Unable to save books " + ex.getMessage());
42 | }
43 | }
44 | }
45 |
46 | }
47 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/data/BookData.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava.data;
2 |
3 | import lombok.Getter;
4 | import lombok.Setter;
5 |
6 | @Getter
7 | @Setter
8 | public class BookData {
9 | private String title;
10 | private String author;
11 | private String coverPhotoURL;
12 | private Long isbnNumber;
13 | private Double price;
14 | private String language;
15 | }
16 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/domain/Book.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava.domain;
2 |
3 | import java.util.UUID;
4 |
5 | import javax.persistence.Entity;
6 | import javax.persistence.GeneratedValue;
7 | import javax.persistence.Id;
8 |
9 | import org.springframework.hateoas.RepresentationModel;
10 |
11 | import com.fasterxml.jackson.annotation.JsonCreator;
12 | import com.fasterxml.jackson.annotation.JsonProperty;
13 |
14 | import lombok.Getter;
15 | import lombok.NoArgsConstructor;
16 | import lombok.Setter;
17 |
18 | @Entity
19 | @Getter
20 | @Setter
21 | @NoArgsConstructor
22 | public class Book extends RepresentationModel {
23 |
24 | @Id
25 | @GeneratedValue
26 | private UUID id;
27 | private String title;
28 | private String author;
29 | private String coverPhotoURL;
30 | private Long isbnNumber;
31 | private Double price;
32 | private String language;
33 | private byte[] base64QRCode;
34 |
35 | @JsonCreator
36 | public Book(@JsonProperty("id") UUID id, @JsonProperty("title") String title, @JsonProperty("author") String author,
37 | @JsonProperty("coverPhotoURL") String coverPhotoURL, @JsonProperty("isbnNumber") Long isbnNumber,
38 | @JsonProperty("price") Double price, @JsonProperty("language") String language) {
39 | this.id = id;
40 | this.title = title;
41 | this.author = author;
42 | this.coverPhotoURL = coverPhotoURL;
43 | this.isbnNumber = isbnNumber;
44 | this.price = price;
45 | this.language = language;
46 | }
47 |
48 | public Book(String title, String author, String coverPhotoURL, Long isbnNumber, Double price, String language) {
49 | this.title = title;
50 | this.author = author;
51 | this.coverPhotoURL = coverPhotoURL;
52 | this.isbnNumber = isbnNumber;
53 | this.price = price;
54 | this.language = language;
55 | }
56 |
57 | }
58 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/exception/ApplicationException.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava.exception;
2 |
3 | public class ApplicationException extends RuntimeException {
4 |
5 | private static final long serialVersionUID = -3101984166649269624L;
6 |
7 | public ApplicationException(String message) {
8 | super(message);
9 | }
10 | }
11 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/exception/BookCustomExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava.exception;
2 |
3 | import org.springframework.http.HttpStatus;
4 | import org.springframework.http.ResponseEntity;
5 | import org.springframework.web.bind.annotation.ControllerAdvice;
6 | import org.springframework.web.bind.annotation.ExceptionHandler;
7 |
8 | import com.mightyjava.util.MethodUtils;
9 |
10 | @ControllerAdvice
11 | public class BookCustomExceptionHandler {
12 |
13 | @ExceptionHandler(value = ApplicationException.class)
14 | public ResponseEntity applicationException(ApplicationException exception) {
15 | HttpStatus status = HttpStatus.BAD_REQUEST;
16 | return new ResponseEntity<>(MethodUtils.prepareErrorJSON(status, exception), status);
17 | }
18 |
19 | @ExceptionHandler(value = BookNotFoundException.class)
20 | public ResponseEntity bookNotFoundException(BookNotFoundException exception) {
21 | HttpStatus status = HttpStatus.NOT_FOUND;
22 | return new ResponseEntity<>(MethodUtils.prepareErrorJSON(status, exception), status);
23 | }
24 |
25 | }
26 |
--------------------------------------------------------------------------------
/src/main/java/com/mightyjava/exception/BookExceptionHandler.java:
--------------------------------------------------------------------------------
1 | package com.mightyjava.exception;
2 |
3 | import org.springframework.http.HttpHeaders;
4 | import org.springframework.http.HttpStatus;
5 | import org.springframework.http.ResponseEntity;
6 | import org.springframework.http.converter.HttpMessageNotReadableException;
7 | import org.springframework.web.bind.annotation.ControllerAdvice;
8 | import org.springframework.web.context.request.WebRequest;
9 | import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
10 |
11 | import com.mightyjava.util.MethodUtils;
12 |
13 | @ControllerAdvice
14 | public class BookExceptionHandler extends ResponseEntityExceptionHandler {
15 |
16 | @Override
17 | protected ResponseEntity