added spring-rest-hal-browser code (#4701)
This commit is contained in:
parent
a9645c0938
commit
ff8b9a8437
|
@ -0,0 +1,53 @@
|
||||||
|
<?xml version="1.0" encoding="UTF-8"?>
|
||||||
|
<project xmlns="http://maven.apache.org/POM/4.0.0"
|
||||||
|
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
|
||||||
|
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
|
||||||
|
<modelVersion>4.0.0</modelVersion>
|
||||||
|
|
||||||
|
<groupId>com.baeldung</groupId>
|
||||||
|
<artifactId>spring-rest-hal-browser</artifactId>
|
||||||
|
<version>1.0-SNAPSHOT</version>
|
||||||
|
<build>
|
||||||
|
<plugins>
|
||||||
|
<plugin>
|
||||||
|
<groupId>org.apache.maven.plugins</groupId>
|
||||||
|
<artifactId>maven-compiler-plugin</artifactId>
|
||||||
|
<configuration>
|
||||||
|
<source>1.8</source>
|
||||||
|
<target>1.8</target>
|
||||||
|
</configuration>
|
||||||
|
</plugin>
|
||||||
|
</plugins>
|
||||||
|
</build>
|
||||||
|
|
||||||
|
<dependencies>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-web</artifactId>
|
||||||
|
<version>2.0.3.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.boot</groupId>
|
||||||
|
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||||
|
<version>2.0.3.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-hal-browser -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>org.springframework.data</groupId>
|
||||||
|
<artifactId>spring-data-rest-hal-browser</artifactId>
|
||||||
|
<version>3.0.8.RELEASE</version>
|
||||||
|
</dependency>
|
||||||
|
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
|
||||||
|
<dependency>
|
||||||
|
<groupId>com.h2database</groupId>
|
||||||
|
<artifactId>h2</artifactId>
|
||||||
|
<version>1.4.197</version>
|
||||||
|
</dependency>
|
||||||
|
|
||||||
|
|
||||||
|
</dependencies>
|
||||||
|
|
||||||
|
|
||||||
|
</project>
|
|
@ -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);
|
||||||
|
}
|
||||||
|
|
||||||
|
}
|
|
@ -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);
|
||||||
|
|
||||||
|
});
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
|
@ -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);
|
||||||
|
}
|
||||||
|
}
|
|
@ -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<Book, Long> {
|
||||||
|
|
||||||
|
@RestResource(rel = "title-contains", path="title-contains")
|
||||||
|
Page<Book> findByTitleContaining(@Param("query") String query, Pageable page);
|
||||||
|
|
||||||
|
@RestResource(rel = "author-contains", path="author-contains", exported = false)
|
||||||
|
Page<Book> findByAuthorContaining(@Param("query") String query, Pageable page);
|
||||||
|
|
||||||
|
}
|
|
@ -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 +
|
||||||
|
'}';
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue