diff --git a/spring-rest-hal-browser/pom.xml b/spring-rest-hal-browser/pom.xml new file mode 100644 index 0000000000..6c56faf0b2 --- /dev/null +++ b/spring-rest-hal-browser/pom.xml @@ -0,0 +1,53 @@ + + + 4.0.0 + + com.baeldung + spring-rest-hal-browser + 1.0-SNAPSHOT + + + + org.apache.maven.plugins + maven-compiler-plugin + + 1.8 + 1.8 + + + + + + + + + org.springframework.boot + spring-boot-starter-web + 2.0.3.RELEASE + + + + org.springframework.boot + spring-boot-starter-data-jpa + 2.0.3.RELEASE + + + + org.springframework.data + spring-data-rest-hal-browser + 3.0.8.RELEASE + + + + com.h2database + h2 + 1.4.197 + + + + + + + \ No newline at end of file diff --git a/spring-rest-hal-browser/src/main/java/com/baeldung/App.java b/spring-rest-hal-browser/src/main/java/com/baeldung/App.java new file mode 100644 index 0000000000..14b6c201d5 --- /dev/null +++ b/spring-rest-hal-browser/src/main/java/com/baeldung/App.java @@ -0,0 +1,13 @@ +package com.baeldung; + +import org.springframework.boot.SpringApplication; +import org.springframework.boot.autoconfigure.SpringBootApplication; + +@SpringBootApplication +public class App { + + public static void main(String[] args) { + SpringApplication.run(App.class); + } + +} diff --git a/spring-rest-hal-browser/src/main/java/com/baeldung/config/DBLoader.java b/spring-rest-hal-browser/src/main/java/com/baeldung/config/DBLoader.java new file mode 100644 index 0000000000..7251ef0e8c --- /dev/null +++ b/spring-rest-hal-browser/src/main/java/com/baeldung/config/DBLoader.java @@ -0,0 +1,108 @@ +package com.baeldung.config; + +import com.baeldung.data.BookRepository; +import com.baeldung.model.Book; +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.boot.ApplicationArguments; +import org.springframework.boot.ApplicationRunner; +import org.springframework.stereotype.Component; + +import java.util.Random; +import java.util.stream.IntStream; + +@Component +public class DBLoader implements ApplicationRunner { + + private final BookRepository bookRepository; + + @Autowired + DBLoader(BookRepository bookRepository){ + this.bookRepository = bookRepository; + } + + + public void run(ApplicationArguments applicationArguments) throws Exception { + + String[] templates = { + "Up and running with %s", + "%s Basics", + "%s for Beginners", + "%s for Neckbeards", + "Under the hood: %s", + "Discovering %s", + "A short guide to %s", + "%s with Baeldung" + }; + + String[] buzzWords = { + "Spring REST Data", + "Java 9", + "Scala", + "Groovy", + "Hibernate", + "Spring HATEOS", + "The HAL Browser", + "Spring webflux", + }; + + String[] authorFirstName = { + "John %s", + "Steve %s", + "Samantha %s", + "Gale %s", + "Tom %s" + }; + + String[] authorLastName = { + "Giles", + "Gill", + "Smith", + "Armstrong" + }; + + String[] blurbs = { + "It was getting dark when the %s %s" , + "Scott was nearly there when he heard that a %s %s", + "Diana was a lovable Java coder until the %s %s", + "The gripping story of a small %s and the day it %s" + }; + + String[] blublMiddles = { + "distaster", + "dog", + "cat", + "turtle", + "hurricane" + }; + + String[] end = { + "hit the school", + "memorised pi to 100 decimal places!", + "became a world champion armwrestler", + "became a Java REST master!!" + }; + + Random random = new Random(); + + IntStream.range(0, 100) + .forEach(i -> { + String template = templates[i % templates.length]; + String buzzword = buzzWords[i % buzzWords.length]; + String blurbStart = blurbs[i % blurbs.length]; + String middle = blublMiddles[i % blublMiddles.length]; + String ending = end[i % end.length]; + String blurb = String.format(blurbStart, middle, ending); + String firstName = authorFirstName[i % authorFirstName.length]; + String lastname = authorLastName[i % authorLastName.length]; + Book book = new Book(String.format(template, buzzword), String.format(firstName, lastname), blurb, random.nextInt(1000-200) + 200); + + bookRepository.save(book); + + System.out.println(book); + + }); + + + + } +} diff --git a/spring-rest-hal-browser/src/main/java/com/baeldung/config/RestConfig.java b/spring-rest-hal-browser/src/main/java/com/baeldung/config/RestConfig.java new file mode 100644 index 0000000000..858371facc --- /dev/null +++ b/spring-rest-hal-browser/src/main/java/com/baeldung/config/RestConfig.java @@ -0,0 +1,23 @@ +package com.baeldung.config; + +import org.springframework.beans.factory.annotation.Autowired; +import org.springframework.context.annotation.Configuration; +import org.springframework.context.annotation.Lazy; +import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener; +import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter; +import org.springframework.validation.Validator; + +@Configuration +public class RestConfig extends RepositoryRestConfigurerAdapter { + + //access to global validator + @Autowired + @Lazy + private Validator validator; + + @Override + public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) { + validatingListener.addValidator("beforeCreate", validator ); + validatingListener.addValidator("beforeSave", validator); + } +} diff --git a/spring-rest-hal-browser/src/main/java/com/baeldung/data/BookRepository.java b/spring-rest-hal-browser/src/main/java/com/baeldung/data/BookRepository.java new file mode 100644 index 0000000000..d8e35974b1 --- /dev/null +++ b/spring-rest-hal-browser/src/main/java/com/baeldung/data/BookRepository.java @@ -0,0 +1,21 @@ +package com.baeldung.data; + +import com.baeldung.model.Book; +import org.springframework.data.domain.Page; +import org.springframework.data.domain.Pageable; +import org.springframework.data.jpa.repository.Query; +import org.springframework.data.repository.PagingAndSortingRepository; +import org.springframework.data.repository.query.Param; +import org.springframework.data.rest.core.annotation.RestResource; +import org.springframework.stereotype.Repository; + +@Repository +public interface BookRepository extends PagingAndSortingRepository { + + @RestResource(rel = "title-contains", path="title-contains") + Page findByTitleContaining(@Param("query") String query, Pageable page); + + @RestResource(rel = "author-contains", path="author-contains", exported = false) + Page findByAuthorContaining(@Param("query") String query, Pageable page); + +} diff --git a/spring-rest-hal-browser/src/main/java/com/baeldung/model/Book.java b/spring-rest-hal-browser/src/main/java/com/baeldung/model/Book.java new file mode 100644 index 0000000000..b1dc1b41f3 --- /dev/null +++ b/spring-rest-hal-browser/src/main/java/com/baeldung/model/Book.java @@ -0,0 +1,86 @@ +package com.baeldung.model; + +import javax.persistence.*; +import javax.validation.constraints.NotNull; + +@Entity +public class Book { + + @Id + @GeneratedValue(strategy = GenerationType.IDENTITY) + private long id; + + @NotNull + @Column(columnDefinition = "VARCHAR", length = 100) + private String title; + + @NotNull + @Column(columnDefinition = "VARCHAR", length = 100) + private String author; + + @Column(columnDefinition = "VARCHAR", length = 1000) + private String blurb; + + private int pages; + + public Book() { + } + + public Book(@NotNull String title, @NotNull String author, String blurb, int pages) { + this.title = title; + this.author = author; + this.blurb = blurb; + this.pages = pages; + } + + public long getId() { + return id; + } + + public void setId(long id) { + this.id = id; + } + + 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 getBlurb() { + return blurb; + } + + public void setBlurb(String blurb) { + this.blurb = blurb; + } + + public int getPages() { + return pages; + } + + public void setPages(int pages) { + this.pages = pages; + } + + @Override + public String toString() { + return "Book{" + + "id=" + id + + ", title='" + title + '\'' + + ", author='" + author + '\'' + + ", blurb='" + blurb + '\'' + + ", pages=" + pages + + '}'; + } +} diff --git a/spring-rest-hal-browser/src/main/resources/application.properties b/spring-rest-hal-browser/src/main/resources/application.properties new file mode 100644 index 0000000000..e69de29bb2