Skip to content

Commit ff8b9a8

Browse files
sjmillingtonpivovarit
authored andcommitted
added spring-rest-hal-browser code (eugenp#4701)
1 parent a9645c0 commit ff8b9a8

7 files changed

Lines changed: 304 additions & 0 deletions

File tree

‎spring-rest-hal-browser/pom.xml‎

Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0"
3+
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
4+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
5+
<modelVersion>4.0.0</modelVersion>
6+
7+
<groupId>com.baeldung</groupId>
8+
<artifactId>spring-rest-hal-browser</artifactId>
9+
<version>1.0-SNAPSHOT</version>
10+
<build>
11+
<plugins>
12+
<plugin>
13+
<groupId>org.apache.maven.plugins</groupId>
14+
<artifactId>maven-compiler-plugin</artifactId>
15+
<configuration>
16+
<source>1.8</source>
17+
<target>1.8</target>
18+
</configuration>
19+
</plugin>
20+
</plugins>
21+
</build>
22+
23+
<dependencies>
24+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-web -->
25+
<dependency>
26+
<groupId>org.springframework.boot</groupId>
27+
<artifactId>spring-boot-starter-web</artifactId>
28+
<version>2.0.3.RELEASE</version>
29+
</dependency>
30+
<!-- https://mvnrepository.com/artifact/org.springframework.boot/spring-boot-starter-data-jpa -->
31+
<dependency>
32+
<groupId>org.springframework.boot</groupId>
33+
<artifactId>spring-boot-starter-data-jpa</artifactId>
34+
<version>2.0.3.RELEASE</version>
35+
</dependency>
36+
<!-- https://mvnrepository.com/artifact/org.springframework.data/spring-data-rest-hal-browser -->
37+
<dependency>
38+
<groupId>org.springframework.data</groupId>
39+
<artifactId>spring-data-rest-hal-browser</artifactId>
40+
<version>3.0.8.RELEASE</version>
41+
</dependency>
42+
<!-- https://mvnrepository.com/artifact/com.h2database/h2 -->
43+
<dependency>
44+
<groupId>com.h2database</groupId>
45+
<artifactId>h2</artifactId>
46+
<version>1.4.197</version>
47+
</dependency>
48+
49+
50+
</dependencies>
51+
52+
53+
</project>
Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package com.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
6+
@SpringBootApplication
7+
public class App {
8+
9+
public static void main(String[] args) {
10+
SpringApplication.run(App.class);
11+
}
12+
13+
}
Lines changed: 108 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,108 @@
1+
package com.baeldung.config;
2+
3+
import com.baeldung.data.BookRepository;
4+
import com.baeldung.model.Book;
5+
import org.springframework.beans.factory.annotation.Autowired;
6+
import org.springframework.boot.ApplicationArguments;
7+
import org.springframework.boot.ApplicationRunner;
8+
import org.springframework.stereotype.Component;
9+
10+
import java.util.Random;
11+
import java.util.stream.IntStream;
12+
13+
@Component
14+
public class DBLoader implements ApplicationRunner {
15+
16+
private final BookRepository bookRepository;
17+
18+
@Autowired
19+
DBLoader(BookRepository bookRepository){
20+
this.bookRepository = bookRepository;
21+
}
22+
23+
24+
public void run(ApplicationArguments applicationArguments) throws Exception {
25+
26+
String[] templates = {
27+
"Up and running with %s",
28+
"%s Basics",
29+
"%s for Beginners",
30+
"%s for Neckbeards",
31+
"Under the hood: %s",
32+
"Discovering %s",
33+
"A short guide to %s",
34+
"%s with Baeldung"
35+
};
36+
37+
String[] buzzWords = {
38+
"Spring REST Data",
39+
"Java 9",
40+
"Scala",
41+
"Groovy",
42+
"Hibernate",
43+
"Spring HATEOS",
44+
"The HAL Browser",
45+
"Spring webflux",
46+
};
47+
48+
String[] authorFirstName = {
49+
"John %s",
50+
"Steve %s",
51+
"Samantha %s",
52+
"Gale %s",
53+
"Tom %s"
54+
};
55+
56+
String[] authorLastName = {
57+
"Giles",
58+
"Gill",
59+
"Smith",
60+
"Armstrong"
61+
};
62+
63+
String[] blurbs = {
64+
"It was getting dark when the %s %s" ,
65+
"Scott was nearly there when he heard that a %s %s",
66+
"Diana was a lovable Java coder until the %s %s",
67+
"The gripping story of a small %s and the day it %s"
68+
};
69+
70+
String[] blublMiddles = {
71+
"distaster",
72+
"dog",
73+
"cat",
74+
"turtle",
75+
"hurricane"
76+
};
77+
78+
String[] end = {
79+
"hit the school",
80+
"memorised pi to 100 decimal places!",
81+
"became a world champion armwrestler",
82+
"became a Java REST master!!"
83+
};
84+
85+
Random random = new Random();
86+
87+
IntStream.range(0, 100)
88+
.forEach(i -> {
89+
String template = templates[i % templates.length];
90+
String buzzword = buzzWords[i % buzzWords.length];
91+
String blurbStart = blurbs[i % blurbs.length];
92+
String middle = blublMiddles[i % blublMiddles.length];
93+
String ending = end[i % end.length];
94+
String blurb = String.format(blurbStart, middle, ending);
95+
String firstName = authorFirstName[i % authorFirstName.length];
96+
String lastname = authorLastName[i % authorLastName.length];
97+
Book book = new Book(String.format(template, buzzword), String.format(firstName, lastname), blurb, random.nextInt(1000-200) + 200);
98+
99+
bookRepository.save(book);
100+
101+
System.out.println(book);
102+
103+
});
104+
105+
106+
107+
}
108+
}
Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
package com.baeldung.config;
2+
3+
import org.springframework.beans.factory.annotation.Autowired;
4+
import org.springframework.context.annotation.Configuration;
5+
import org.springframework.context.annotation.Lazy;
6+
import org.springframework.data.rest.core.event.ValidatingRepositoryEventListener;
7+
import org.springframework.data.rest.webmvc.config.RepositoryRestConfigurerAdapter;
8+
import org.springframework.validation.Validator;
9+
10+
@Configuration
11+
public class RestConfig extends RepositoryRestConfigurerAdapter {
12+
13+
//access to global validator
14+
@Autowired
15+
@Lazy
16+
private Validator validator;
17+
18+
@Override
19+
public void configureValidatingRepositoryEventListener(ValidatingRepositoryEventListener validatingListener) {
20+
validatingListener.addValidator("beforeCreate", validator );
21+
validatingListener.addValidator("beforeSave", validator);
22+
}
23+
}
Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package com.baeldung.data;
2+
3+
import com.baeldung.model.Book;
4+
import org.springframework.data.domain.Page;
5+
import org.springframework.data.domain.Pageable;
6+
import org.springframework.data.jpa.repository.Query;
7+
import org.springframework.data.repository.PagingAndSortingRepository;
8+
import org.springframework.data.repository.query.Param;
9+
import org.springframework.data.rest.core.annotation.RestResource;
10+
import org.springframework.stereotype.Repository;
11+
12+
@Repository
13+
public interface BookRepository extends PagingAndSortingRepository<Book, Long> {
14+
15+
@RestResource(rel = "title-contains", path="title-contains")
16+
Page<Book> findByTitleContaining(@Param("query") String query, Pageable page);
17+
18+
@RestResource(rel = "author-contains", path="author-contains", exported = false)
19+
Page<Book> findByAuthorContaining(@Param("query") String query, Pageable page);
20+
21+
}
Lines changed: 86 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
package com.baeldung.model;
2+
3+
import javax.persistence.*;
4+
import javax.validation.constraints.NotNull;
5+
6+
@Entity
7+
public class Book {
8+
9+
@Id
10+
@GeneratedValue(strategy = GenerationType.IDENTITY)
11+
private long id;
12+
13+
@NotNull
14+
@Column(columnDefinition = "VARCHAR", length = 100)
15+
private String title;
16+
17+
@NotNull
18+
@Column(columnDefinition = "VARCHAR", length = 100)
19+
private String author;
20+
21+
@Column(columnDefinition = "VARCHAR", length = 1000)
22+
private String blurb;
23+
24+
private int pages;
25+
26+
public Book() {
27+
}
28+
29+
public Book(@NotNull String title, @NotNull String author, String blurb, int pages) {
30+
this.title = title;
31+
this.author = author;
32+
this.blurb = blurb;
33+
this.pages = pages;
34+
}
35+
36+
public long getId() {
37+
return id;
38+
}
39+
40+
public void setId(long id) {
41+
this.id = id;
42+
}
43+
44+
public String getTitle() {
45+
return title;
46+
}
47+
48+
public void setTitle(String title) {
49+
this.title = title;
50+
}
51+
52+
public String getAuthor() {
53+
return author;
54+
}
55+
56+
public void setAuthor(String author) {
57+
this.author = author;
58+
}
59+
60+
public String getBlurb() {
61+
return blurb;
62+
}
63+
64+
public void setBlurb(String blurb) {
65+
this.blurb = blurb;
66+
}
67+
68+
public int getPages() {
69+
return pages;
70+
}
71+
72+
public void setPages(int pages) {
73+
this.pages = pages;
74+
}
75+
76+
@Override
77+
public String toString() {
78+
return "Book{" +
79+
"id=" + id +
80+
", title='" + title + '\'' +
81+
", author='" + author + '\'' +
82+
", blurb='" + blurb + '\'' +
83+
", pages=" + pages +
84+
'}';
85+
}
86+
}

‎spring-rest-hal-browser/src/main/resources/application.properties‎

Whitespace-only changes.

0 commit comments

Comments
 (0)