moved spring-rest-hal-browser to spring-data-rest module (#10019)

* moved spring-rest-hal-browser to spring-data-rest module

* removed module from build
This commit is contained in:
Amit Pandey
2020-09-14 21:53:09 +05:30
committed by GitHub
parent f47741a9b1
commit d73bc7adb0
12 changed files with 22 additions and 94 deletions

View File

@@ -12,6 +12,7 @@ This module contains articles about Spring Data REST
- [Customizing HTTP Endpoints in Spring Data REST](https://www.baeldung.com/spring-data-rest-customize-http-endpoints)
- [Spring Boot with SQLite](https://www.baeldung.com/spring-boot-sqlite)
- [Spring Data Web Support](https://www.baeldung.com/spring-data-web-support)
- [Spring REST and HAL Browser](https://www.baeldung.com/spring-rest-hal)
### The Course
The "REST With Spring" Classes: http://bit.ly/restwithspring

View File

@@ -32,6 +32,15 @@
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-jpa</artifactId>
</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>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-validation</artifactId>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-autoconfigure</artifactId>

View File

@@ -0,0 +1,13 @@
package com.baeldung.halbrowser;
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);
}
}

View File

@@ -0,0 +1,109 @@
package com.baeldung.halbrowser.config;
import java.util.Random;
import java.util.stream.IntStream;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Component;
import com.baeldung.halbrowser.data.BookRepository;
import com.baeldung.halbrowser.model.Book;
@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);
});
}
}

View File

@@ -0,0 +1,23 @@
package com.baeldung.halbrowser.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);
}
}

View File

@@ -0,0 +1,21 @@
package com.baeldung.halbrowser.data;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
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;
import com.baeldung.halbrowser.model.Book;
@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);
}

View File

@@ -0,0 +1,86 @@
package com.baeldung.halbrowser.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 +
'}';
}
}