├── .gitignore
├── README.md
├── pom.xml
└── src
├── main
├── java
│ └── com
│ │ └── baeldung
│ │ └── library
│ │ ├── LibraryApplication.java
│ │ ├── domain
│ │ ├── Author.java
│ │ └── Book.java
│ │ └── repo
│ │ ├── AuthorRepository.java
│ │ └── BookRepository.java
└── resources
│ └── application.properties
└── test
└── java
└── com
└── baeldung
└── library
└── LibraryApplicationTests.java
/.gitignore:
--------------------------------------------------------------------------------
1 | */bin/*
2 |
3 | *.class
4 |
5 | # Package Files #
6 | *.war
7 | *.ear
8 |
9 |
10 | # Eclipse
11 | .settings/
12 | *.project
13 | *.classpath
14 | .prefs
15 | *.prefs
16 | .metadata/
17 |
18 | # Intellij
19 | .idea/
20 | *.iml
21 | *.iws
22 |
23 | # Mac
24 | .DS_Store
25 |
26 | # Maven
27 | log/
28 | target/
29 |
30 | *.springBeans
31 |
--------------------------------------------------------------------------------
/README.md:
--------------------------------------------------------------------------------
1 | # Library Example App
2 |
3 | A very simple Spring Boot 2 - Library example app: https://www.youtube.com/watch?v=-f-7l8X716k
4 |
--------------------------------------------------------------------------------
/pom.xml:
--------------------------------------------------------------------------------
1 |
2 |
5 | 4.0.0
6 |
7 | com.baeldung
8 | library-reference
9 | 0.0.1-SNAPSHOT
10 | jar
11 |
12 | library
13 | Demo project for Spring Boot
14 |
15 |
16 | org.springframework.boot
17 | spring-boot-starter-parent
18 | 2.0.1.RELEASE
19 |
20 |
21 |
22 |
23 | UTF-8
24 | UTF-8
25 | 1.8
26 |
27 |
28 |
29 |
30 | org.springframework.boot
31 | spring-boot-starter-actuator
32 |
33 |
34 | org.springframework.boot
35 | spring-boot-starter-data-jpa
36 |
37 |
38 | org.springframework.boot
39 | spring-boot-starter-data-rest
40 |
41 |
42 | org.springframework.boot
43 | spring-boot-starter-web
44 |
45 |
46 |
47 | org.springframework.boot
48 | spring-boot-devtools
49 | runtime
50 |
51 |
52 | com.h2database
53 | h2
54 | runtime
55 |
56 |
57 | org.projectlombok
58 | lombok
59 | true
60 |
61 |
62 | com.google.guava
63 | guava
64 | 25.0-jre
65 |
66 |
67 |
68 | org.springframework.boot
69 | spring-boot-starter-test
70 | test
71 |
72 |
73 | org.assertj
74 | assertj-core
75 |
76 |
77 |
78 |
79 |
80 |
81 |
82 |
83 | org.springframework.boot
84 | spring-boot-maven-plugin
85 |
86 |
87 |
88 |
89 |
90 |
91 |
--------------------------------------------------------------------------------
/src/main/java/com/baeldung/library/LibraryApplication.java:
--------------------------------------------------------------------------------
1 | package com.baeldung.library;
2 |
3 | import org.springframework.beans.factory.annotation.Autowired;
4 | import org.springframework.boot.ApplicationArguments;
5 | import org.springframework.boot.ApplicationRunner;
6 | import org.springframework.boot.SpringApplication;
7 | import org.springframework.boot.autoconfigure.SpringBootApplication;
8 | import org.springframework.stereotype.Component;
9 |
10 | import com.baeldung.library.domain.Author;
11 | import com.baeldung.library.domain.Book;
12 | import com.baeldung.library.repo.AuthorRepository;
13 | import com.google.common.collect.Lists;
14 |
15 | @SpringBootApplication
16 | public class LibraryApplication {
17 |
18 | @Autowired
19 | private AuthorRepository authorRepo;
20 |
21 | @Component
22 | class DataSetup implements ApplicationRunner {
23 | @Override
24 | public void run(ApplicationArguments args) throws Exception {
25 | final Book goneWithTheWindBook = Book.builder().title("Gone with the Wind").isbn("9787806571491").build();
26 | final Author goneWithTheWindAuthor = Author.builder().name("Margaret Mitchell").books(Lists.newArrayList(goneWithTheWindBook)).build();
27 |
28 | authorRepo.save(goneWithTheWindAuthor);
29 | }
30 | }
31 |
32 | public static void main(String[] args) {
33 | SpringApplication.run(LibraryApplication.class, args);
34 | }
35 |
36 | }
37 |
--------------------------------------------------------------------------------
/src/main/java/com/baeldung/library/domain/Author.java:
--------------------------------------------------------------------------------
1 | package com.baeldung.library.domain;
2 |
3 | import java.util.List;
4 |
5 | import javax.persistence.CascadeType;
6 | import javax.persistence.Entity;
7 | import javax.persistence.GeneratedValue;
8 | import javax.persistence.Id;
9 | import javax.persistence.OneToMany;
10 | import javax.validation.constraints.NotNull;
11 |
12 | import lombok.AllArgsConstructor;
13 | import lombok.Builder;
14 | import lombok.Data;
15 | import lombok.NoArgsConstructor;
16 |
17 | @Builder
18 | @NoArgsConstructor
19 | @AllArgsConstructor
20 | @Data
21 | @Entity
22 | public class Author {
23 |
24 | @Id
25 | @GeneratedValue
26 | private long id;
27 |
28 | @NotNull
29 | private String name;
30 |
31 | @OneToMany(cascade = CascadeType.ALL)
32 | private List books;
33 |
34 | }
35 |
--------------------------------------------------------------------------------
/src/main/java/com/baeldung/library/domain/Book.java:
--------------------------------------------------------------------------------
1 | package com.baeldung.library.domain;
2 |
3 | import javax.persistence.Entity;
4 | import javax.persistence.GeneratedValue;
5 | import javax.persistence.Id;
6 | import javax.validation.constraints.NotNull;
7 |
8 | import lombok.AllArgsConstructor;
9 | import lombok.Builder;
10 | import lombok.Data;
11 | import lombok.NoArgsConstructor;
12 |
13 | @Builder
14 | @NoArgsConstructor
15 | @AllArgsConstructor
16 | @Data
17 | @Entity
18 | public class Book {
19 |
20 | @Id
21 | @GeneratedValue
22 | private long id;
23 |
24 | @NotNull
25 | private String isbn;
26 |
27 | @NotNull
28 | private String title;
29 |
30 | }
31 |
--------------------------------------------------------------------------------
/src/main/java/com/baeldung/library/repo/AuthorRepository.java:
--------------------------------------------------------------------------------
1 | package com.baeldung.library.repo;
2 |
3 | import org.springframework.data.repository.PagingAndSortingRepository;
4 | import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5 |
6 | import com.baeldung.library.domain.Author;
7 |
8 | @RepositoryRestResource(collectionResourceRel = "authors", path = "authors")
9 | public interface AuthorRepository extends PagingAndSortingRepository {
10 | //
11 | }
--------------------------------------------------------------------------------
/src/main/java/com/baeldung/library/repo/BookRepository.java:
--------------------------------------------------------------------------------
1 | package com.baeldung.library.repo;
2 |
3 | import org.springframework.data.repository.PagingAndSortingRepository;
4 | import org.springframework.data.rest.core.annotation.RepositoryRestResource;
5 |
6 | import com.baeldung.library.domain.Book;
7 |
8 | @RepositoryRestResource(collectionResourceRel = "books", path = "books")
9 | public interface BookRepository extends PagingAndSortingRepository {
10 | //
11 | }
--------------------------------------------------------------------------------
/src/main/resources/application.properties:
--------------------------------------------------------------------------------
1 | logging.level.org.springframework.data.rest.webmvc=WARN
2 |
--------------------------------------------------------------------------------
/src/test/java/com/baeldung/library/LibraryApplicationTests.java:
--------------------------------------------------------------------------------
1 | package com.baeldung.library;
2 |
3 | import static org.hamcrest.CoreMatchers.not;
4 | import static org.hamcrest.Matchers.emptyIterable;
5 | import static org.junit.Assert.assertThat;
6 |
7 | import org.junit.Test;
8 | import org.junit.runner.RunWith;
9 | import org.springframework.beans.factory.annotation.Autowired;
10 | import org.springframework.boot.test.context.SpringBootTest;
11 | import org.springframework.test.context.junit4.SpringRunner;
12 |
13 | import com.baeldung.library.repo.BookRepository;
14 |
15 | @RunWith(SpringRunner.class)
16 | @SpringBootTest
17 | public class LibraryApplicationTests {
18 |
19 | @Autowired
20 | private BookRepository bookRepo;
21 |
22 | @Test
23 | public void contextLoads() {
24 | //
25 | }
26 |
27 | @Test
28 | public void persistenceWorks() {
29 | bookRepo.findAll();
30 | }
31 |
32 | @Test
33 | public void dataExists() {
34 | assertThat(bookRepo.findAll(), not(emptyIterable()));
35 | }
36 |
37 | }
38 |
--------------------------------------------------------------------------------