Skip to content

Commit f73893b

Browse files
Doha2012Eugen
authored andcommitted
spring boot bootstrap (eugenp#2031)
* minor logging fix * spring security sso * use basic auth * use form login * cleanup * cleanup * final cleanup * second client app for sso * spring boot bootstrap * add logic * cleanup * add simple controller * add thymeleaf and security * minor fix * minor fix * add more boot properties
1 parent 80f6331 commit f73893b

15 files changed

Lines changed: 594 additions & 0 deletions

File tree

‎spring-boot-bootstrap/.gitignore‎

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
target/
2+
!.mvn/wrapper/maven-wrapper.jar
3+
4+
### STS ###
5+
.apt_generated
6+
.classpath
7+
.factorypath
8+
.project
9+
.settings
10+
.springBeans
11+
12+
### IntelliJ IDEA ###
13+
.idea
14+
*.iws
15+
*.iml
16+
*.ipr
17+
18+
### NetBeans ###
19+
nbproject/private/
20+
build/
21+
nbbuild/
22+
dist/
23+
nbdist/
24+
.nb-gradle/

‎spring-boot-bootstrap/pom.xml‎

Lines changed: 75 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,75 @@
1+
<?xml version="1.0" encoding="UTF-8"?>
2+
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
3+
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
4+
<modelVersion>4.0.0</modelVersion>
5+
6+
<groupId>org.baeldung</groupId>
7+
<artifactId>spring-boot-bootstrap</artifactId>
8+
<version>0.0.1-SNAPSHOT</version>
9+
<packaging>jar</packaging>
10+
11+
<name>spring-boot-bootstrap</name>
12+
<description>Demo project for Spring Boot</description>
13+
14+
<parent>
15+
<groupId>org.springframework.boot</groupId>
16+
<artifactId>spring-boot-starter-parent</artifactId>
17+
<version>1.5.3.RELEASE</version>
18+
<relativePath /> <!-- lookup parent from repository -->
19+
</parent>
20+
21+
<properties>
22+
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
23+
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
24+
<java.version>1.8</java.version>
25+
</properties>
26+
27+
<dependencies>
28+
<dependency>
29+
<groupId>org.springframework.boot</groupId>
30+
<artifactId>spring-boot-starter-web</artifactId>
31+
</dependency>
32+
<dependency>
33+
<groupId>org.springframework.boot</groupId>
34+
<artifactId>spring-boot-starter-thymeleaf</artifactId>
35+
</dependency>
36+
<dependency>
37+
<groupId>org.springframework.boot</groupId>
38+
<artifactId>spring-boot-starter-data-jpa</artifactId>
39+
</dependency>
40+
<dependency>
41+
<groupId>com.h2database</groupId>
42+
<artifactId>h2</artifactId>
43+
</dependency>
44+
<dependency>
45+
<groupId>org.springframework.boot</groupId>
46+
<artifactId>spring-boot-starter-security</artifactId>
47+
</dependency>
48+
49+
50+
<dependency>
51+
<groupId>org.springframework.boot</groupId>
52+
<artifactId>spring-boot-starter-test</artifactId>
53+
<scope>test</scope>
54+
</dependency>
55+
56+
<dependency>
57+
<groupId>io.rest-assured</groupId>
58+
<artifactId>rest-assured</artifactId>
59+
<version>3.0.3</version>
60+
<scope>test</scope>
61+
</dependency>
62+
63+
</dependencies>
64+
65+
<build>
66+
<plugins>
67+
<plugin>
68+
<groupId>org.springframework.boot</groupId>
69+
<artifactId>spring-boot-maven-plugin</artifactId>
70+
</plugin>
71+
</plugins>
72+
</build>
73+
74+
75+
</project>
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung;
2+
3+
import org.springframework.boot.SpringApplication;
4+
import org.springframework.boot.autoconfigure.SpringBootApplication;
5+
import org.springframework.boot.autoconfigure.domain.EntityScan;
6+
import org.springframework.context.annotation.ComponentScan;
7+
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
8+
9+
@SpringBootApplication
10+
@ComponentScan("org.baeldung")
11+
@EnableJpaRepositories("org.baeldung.persistence.repo")
12+
@EntityScan("org.baeldung.persistence.model")
13+
public class Application {
14+
15+
public static void main(String[] args) {
16+
SpringApplication.run(Application.class, args);
17+
}
18+
19+
}
Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
package org.baeldung.persistence.model;
2+
3+
import javax.persistence.Column;
4+
import javax.persistence.Entity;
5+
import javax.persistence.GeneratedValue;
6+
import javax.persistence.GenerationType;
7+
import javax.persistence.Id;
8+
9+
@Entity
10+
public class Book {
11+
@Id
12+
@GeneratedValue(strategy = GenerationType.AUTO)
13+
private long id;
14+
15+
@Column(nullable = false, unique = true)
16+
private String title;
17+
18+
@Column(nullable = false)
19+
private String author;
20+
21+
//
22+
23+
public Book() {
24+
super();
25+
}
26+
27+
public Book(String title, String author) {
28+
super();
29+
this.title = title;
30+
this.author = author;
31+
}
32+
33+
public long getId() {
34+
return id;
35+
}
36+
37+
public void setId(long id) {
38+
this.id = id;
39+
}
40+
41+
public String getTitle() {
42+
return title;
43+
}
44+
45+
public void setTitle(String title) {
46+
this.title = title;
47+
}
48+
49+
public String getAuthor() {
50+
return author;
51+
}
52+
53+
public void setAuthor(String author) {
54+
this.author = author;
55+
}
56+
57+
@Override
58+
public int hashCode() {
59+
final int prime = 31;
60+
int result = 1;
61+
result = prime * result + ((author == null) ? 0 : author.hashCode());
62+
result = prime * result + (int) (id ^ (id >>> 32));
63+
result = prime * result + ((title == null) ? 0 : title.hashCode());
64+
return result;
65+
}
66+
67+
@Override
68+
public boolean equals(Object obj) {
69+
if (this == obj)
70+
return true;
71+
if (obj == null)
72+
return false;
73+
if (getClass() != obj.getClass())
74+
return false;
75+
Book other = (Book) obj;
76+
if (author == null) {
77+
if (other.author != null)
78+
return false;
79+
} else if (!author.equals(other.author))
80+
return false;
81+
if (id != other.id)
82+
return false;
83+
if (title == null) {
84+
if (other.title != null)
85+
return false;
86+
} else if (!title.equals(other.title))
87+
return false;
88+
return true;
89+
}
90+
91+
@Override
92+
public String toString() {
93+
return "Book [id=" + id + ", title=" + title + ", author=" + author + "]";
94+
}
95+
96+
}
Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
package org.baeldung.persistence.repo;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.model.Book;
6+
import org.springframework.data.repository.CrudRepository;
7+
8+
public interface BookRepository extends CrudRepository<Book, Long> {
9+
List<Book> findByTitle(String title);
10+
}
Lines changed: 74 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,74 @@
1+
package org.baeldung.web;
2+
3+
import java.util.List;
4+
5+
import org.baeldung.persistence.model.Book;
6+
import org.baeldung.persistence.repo.BookRepository;
7+
import org.baeldung.web.exception.BookIdMismatchException;
8+
import org.baeldung.web.exception.BookNotFoundException;
9+
import org.springframework.beans.factory.annotation.Autowired;
10+
import org.springframework.http.HttpStatus;
11+
import org.springframework.web.bind.annotation.DeleteMapping;
12+
import org.springframework.web.bind.annotation.GetMapping;
13+
import org.springframework.web.bind.annotation.PathVariable;
14+
import org.springframework.web.bind.annotation.PostMapping;
15+
import org.springframework.web.bind.annotation.PutMapping;
16+
import org.springframework.web.bind.annotation.RequestBody;
17+
import org.springframework.web.bind.annotation.RequestMapping;
18+
import org.springframework.web.bind.annotation.ResponseStatus;
19+
import org.springframework.web.bind.annotation.RestController;
20+
21+
@RestController
22+
@RequestMapping("/api/books")
23+
public class BookController {
24+
25+
@Autowired
26+
private BookRepository bookRepository;
27+
28+
@GetMapping
29+
public Iterable<Book> findAll() {
30+
return bookRepository.findAll();
31+
}
32+
33+
@GetMapping("/title/{bookTitle}")
34+
public List<Book> findByTitle(@PathVariable String bookTitle) {
35+
return bookRepository.findByTitle(bookTitle);
36+
}
37+
38+
@GetMapping("/{id}")
39+
public Book findOne(@PathVariable Long id) {
40+
final Book book = bookRepository.findOne(id);
41+
if (book == null) {
42+
throw new BookNotFoundException();
43+
}
44+
return book;
45+
}
46+
47+
@PostMapping
48+
@ResponseStatus(HttpStatus.CREATED)
49+
public Book create(@RequestBody Book book) {
50+
return bookRepository.save(book);
51+
}
52+
53+
@DeleteMapping("/{id}")
54+
public void delete(@PathVariable Long id) {
55+
final Book book = bookRepository.findOne(id);
56+
if (book == null) {
57+
throw new BookNotFoundException();
58+
}
59+
bookRepository.delete(id);
60+
}
61+
62+
@PutMapping("/{id}")
63+
public Book updateBook(@RequestBody Book book, @PathVariable Long id) {
64+
if (book.getId() != id) {
65+
throw new BookIdMismatchException();
66+
}
67+
final Book old = bookRepository.findOne(id);
68+
if (old == null) {
69+
throw new BookNotFoundException();
70+
}
71+
return bookRepository.save(book);
72+
}
73+
74+
}
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
package org.baeldung.web;
2+
3+
import org.baeldung.web.exception.BookIdMismatchException;
4+
import org.baeldung.web.exception.BookNotFoundException;
5+
import org.hibernate.exception.ConstraintViolationException;
6+
import org.springframework.dao.DataIntegrityViolationException;
7+
import org.springframework.http.HttpHeaders;
8+
import org.springframework.http.HttpStatus;
9+
import org.springframework.http.ResponseEntity;
10+
import org.springframework.web.bind.annotation.ControllerAdvice;
11+
import org.springframework.web.bind.annotation.ExceptionHandler;
12+
import org.springframework.web.context.request.WebRequest;
13+
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
14+
15+
@ControllerAdvice
16+
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
17+
18+
public RestExceptionHandler() {
19+
super();
20+
}
21+
22+
@ExceptionHandler({ BookNotFoundException.class })
23+
protected ResponseEntity<Object> handleNotFound(Exception ex, WebRequest request) {
24+
return handleExceptionInternal(ex, "Book not found", new HttpHeaders(), HttpStatus.NOT_FOUND, request);
25+
}
26+
27+
@ExceptionHandler({ BookIdMismatchException.class, ConstraintViolationException.class, DataIntegrityViolationException.class })
28+
public ResponseEntity<Object> handleBadRequest(Exception ex, WebRequest request) {
29+
return handleExceptionInternal(ex, ex.getLocalizedMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
30+
}
31+
32+
}
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package org.baeldung.web;
2+
3+
import org.springframework.beans.factory.annotation.Value;
4+
import org.springframework.stereotype.Controller;
5+
import org.springframework.ui.Model;
6+
import org.springframework.web.bind.annotation.RequestMapping;
7+
8+
@Controller
9+
public class SimpleController {
10+
@Value("${spring.application.name}")
11+
String appName;
12+
13+
@RequestMapping("/")
14+
public String homePage(Model model) {
15+
model.addAttribute("appName", appName);
16+
return "home";
17+
}
18+
19+
}
Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
package org.baeldung.web.exception;
2+
3+
public class BookIdMismatchException extends RuntimeException {
4+
5+
public BookIdMismatchException() {
6+
super();
7+
}
8+
9+
public BookIdMismatchException(final String message, final Throwable cause) {
10+
super(message, cause);
11+
}
12+
13+
public BookIdMismatchException(final String message) {
14+
super(message);
15+
}
16+
17+
public BookIdMismatchException(final Throwable cause) {
18+
super(cause);
19+
}
20+
}

0 commit comments

Comments
 (0)