BAEL-20869 Move remaining spring boot modules
This commit is contained in:
24
spring-boot-modules/spring-boot-bootstrap/.gitignore
vendored
Normal file
24
spring-boot-modules/spring-boot-bootstrap/.gitignore
vendored
Normal file
@@ -0,0 +1,24 @@
|
||||
target/
|
||||
!.mvn/wrapper/maven-wrapper.jar
|
||||
|
||||
### STS ###
|
||||
.apt_generated
|
||||
.classpath
|
||||
.factorypath
|
||||
.project
|
||||
.settings
|
||||
.springBeans
|
||||
|
||||
### IntelliJ IDEA ###
|
||||
.idea
|
||||
*.iws
|
||||
*.iml
|
||||
*.ipr
|
||||
|
||||
### NetBeans ###
|
||||
nbproject/private/
|
||||
build/
|
||||
nbbuild/
|
||||
dist/
|
||||
nbdist/
|
||||
.nb-gradle/
|
||||
12
spring-boot-modules/spring-boot-bootstrap/README.md
Normal file
12
spring-boot-modules/spring-boot-bootstrap/README.md
Normal file
@@ -0,0 +1,12 @@
|
||||
## Spring Boot Bootstrap
|
||||
|
||||
This module contains articles about bootstrapping Spring Boot applications.
|
||||
|
||||
### Relevant Articles:
|
||||
- [Spring Boot Tutorial – Bootstrap a Simple Application](https://www.baeldung.com/spring-boot-start)
|
||||
- [Thin JARs with Spring Boot](https://www.baeldung.com/spring-boot-thin-jar)
|
||||
- [Deploying a Spring Boot Application to Cloud Foundry](https://www.baeldung.com/spring-boot-app-deploy-to-cloud-foundry)
|
||||
- [Deploy a Spring Boot Application to Google App Engine](https://www.baeldung.com/spring-boot-google-app-engine)
|
||||
- [Deploy a Spring Boot Application to OpenShift](https://www.baeldung.com/spring-boot-deploy-openshift)
|
||||
- [Deploy a Spring Boot Application to AWS Beanstalk](https://www.baeldung.com/spring-boot-deploy-aws-beanstalk)
|
||||
- [Guide to @SpringBootConfiguration in Spring Boot](https://www.baeldung.com/springbootconfiguration-annotation)
|
||||
10
spring-boot-modules/spring-boot-bootstrap/cloudfoundry/manifest.yml
Executable file
10
spring-boot-modules/spring-boot-bootstrap/cloudfoundry/manifest.yml
Executable file
@@ -0,0 +1,10 @@
|
||||
---
|
||||
applications:
|
||||
- name: spring-boot-bootstrap
|
||||
memory: 768M
|
||||
random-route: true
|
||||
path: ../target/spring-boot-bootstrap-cf.jar
|
||||
env:
|
||||
SPRING_PROFILES_ACTIVE: cloud,mysql
|
||||
services:
|
||||
- spring-bootstrap-db
|
||||
@@ -0,0 +1,7 @@
|
||||
apiVersion: v1
|
||||
kind: ConfigMap
|
||||
metadata:
|
||||
name: spring-boot-bootstrap
|
||||
data:
|
||||
application.properties: |-
|
||||
spring.datasource.url=jdbc:mysql://baeldung-db:3306/baeldung_db
|
||||
335
spring-boot-modules/spring-boot-bootstrap/pom.xml
Normal file
335
spring-boot-modules/spring-boot-bootstrap/pom.xml
Normal file
@@ -0,0 +1,335 @@
|
||||
<?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>
|
||||
<artifactId>spring-boot-bootstrap</artifactId>
|
||||
<name>spring-boot-bootstrap</name>
|
||||
<packaging>jar</packaging>
|
||||
<description>Demo project for Spring Boot</description>
|
||||
|
||||
<parent>
|
||||
<groupId>com.baeldung</groupId>
|
||||
<artifactId>parent-boot-2</artifactId>
|
||||
<version>0.0.1-SNAPSHOT</version>
|
||||
<relativePath>../../parent-boot-2</relativePath>
|
||||
</parent>
|
||||
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-web</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-thymeleaf</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-data-jpa</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>com.h2database</groupId>
|
||||
<artifactId>h2</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>mysql</groupId>
|
||||
<artifactId>mysql-connector-java</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-security</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-test</artifactId>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>io.rest-assured</groupId>
|
||||
<artifactId>rest-assured</artifactId>
|
||||
<version>${rest-assured.version}</version>
|
||||
<scope>test</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>javax.servlet</groupId>
|
||||
<artifactId>javax.servlet-api</artifactId>
|
||||
<version>${servlet.version}</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
|
||||
<profiles>
|
||||
<profile>
|
||||
<id>beanstalk</id>
|
||||
<build>
|
||||
<finalName>${project.name}-eb</finalName>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/cloud/config/*.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>openshift</id>
|
||||
<properties>
|
||||
<spring-cloud-k8s.version>0.3.0.RELEASE</spring-cloud-k8s.version>
|
||||
<spring-cloud.version>Greenwich.RELEASE</spring-cloud.version>
|
||||
<fabric8.maven.plugin.version>3.5.37</fabric8.maven.plugin.version>
|
||||
</properties>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-kubernetes-dependencies</artifactId>
|
||||
<version>${spring-cloud-k8s.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>${spring-cloud.version}</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter-kubernetes-config</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-actuator</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-actuator</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<excludes>
|
||||
<exclude>**/logback.xml</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/cloud/config/*.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>io.fabric8</groupId>
|
||||
<artifactId>fabric8-maven-plugin</artifactId>
|
||||
<version>${fabric8.maven.plugin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<id>fmp</id>
|
||||
<goals>
|
||||
<goal>resource</goal>
|
||||
<goal>build</goal>
|
||||
</goals>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>cloud-gcp</id>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Greenwich.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-gcp-starter</artifactId>
|
||||
<version>1.0.0.RELEASE</version>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-gcp-starter-sql-mysql</artifactId>
|
||||
<version>1.0.0.RELEASE</version>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>${project.name}-gcp</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<excludes>
|
||||
<exclude>**/logback.xml</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>com.google.cloud.tools</groupId>
|
||||
<artifactId>appengine-maven-plugin</artifactId>
|
||||
<version>1.3.2</version>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>cloudfoundry</id>
|
||||
<dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-dependencies</artifactId>
|
||||
<version>Greenwich.RELEASE</version>
|
||||
<type>pom</type>
|
||||
<scope>import</scope>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
</dependencyManagement>
|
||||
<dependencies>
|
||||
<dependency>
|
||||
<groupId>org.springframework.cloud</groupId>
|
||||
<artifactId>spring-cloud-starter</artifactId>
|
||||
</dependency>
|
||||
<dependency>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-starter-cloud-connectors</artifactId>
|
||||
</dependency>
|
||||
</dependencies>
|
||||
<build>
|
||||
<finalName>${project.name}-cf</finalName>
|
||||
<resources>
|
||||
<resource>
|
||||
<directory>src/main/resources</directory>
|
||||
<excludes>
|
||||
<exclude>**/logback.xml</exclude>
|
||||
</excludes>
|
||||
</resource>
|
||||
</resources>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot</groupId>
|
||||
<artifactId>spring-boot-maven-plugin</artifactId>
|
||||
</plugin>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/cloud/config/*.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>autoconfiguration</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-surefire-plugin</artifactId>
|
||||
<executions>
|
||||
<execution>
|
||||
<phase>integration-test</phase>
|
||||
<goals>
|
||||
<goal>test</goal>
|
||||
</goals>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/*LiveTest.java</exclude>
|
||||
<exclude>**/*IntegrationTest.java</exclude>
|
||||
<exclude>**/*IntTest.java</exclude>
|
||||
</excludes>
|
||||
<includes>
|
||||
<include>**/AutoconfigurationTest.java</include>
|
||||
</includes>
|
||||
</configuration>
|
||||
</execution>
|
||||
</executions>
|
||||
<configuration>
|
||||
<systemPropertyVariables>
|
||||
<test.mime>json</test.mime>
|
||||
</systemPropertyVariables>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
<profile>
|
||||
<id>thin-jar</id>
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.springframework.boot.experimental</groupId>
|
||||
<artifactId>spring-boot-thin-maven-plugin</artifactId>
|
||||
<version>${thin.version}</version>
|
||||
<executions>
|
||||
<execution>
|
||||
<!-- Download the dependencies at build time -->
|
||||
<id>resolve</id>
|
||||
<goals>
|
||||
<goal>resolve</goal>
|
||||
</goals>
|
||||
<inherited>false</inherited>
|
||||
</execution>
|
||||
</executions>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
</profile>
|
||||
</profiles>
|
||||
|
||||
<build>
|
||||
<plugins>
|
||||
<plugin>
|
||||
<groupId>org.apache.maven.plugins</groupId>
|
||||
<artifactId>maven-compiler-plugin</artifactId>
|
||||
<configuration>
|
||||
<excludes>
|
||||
<exclude>**/cloud/config/*.java</exclude>
|
||||
</excludes>
|
||||
</configuration>
|
||||
</plugin>
|
||||
</plugins>
|
||||
</build>
|
||||
|
||||
<properties>
|
||||
<servlet.version>4.0.0</servlet.version>
|
||||
</properties>
|
||||
|
||||
</project>
|
||||
@@ -0,0 +1,19 @@
|
||||
runtime: java
|
||||
env: flex
|
||||
runtime_config:
|
||||
jdk: openjdk8
|
||||
env_variables:
|
||||
SPRING_PROFILES_ACTIVE: "gcp,mysql"
|
||||
handlers:
|
||||
- url: /.*
|
||||
script: this field is required, but ignored
|
||||
resources:
|
||||
cpu: 2
|
||||
memory_gb: 2
|
||||
disk_size_gb: 10
|
||||
volumes:
|
||||
- name: ramdisk1
|
||||
volume_type: tmpfs
|
||||
size_gb: 0.5
|
||||
manual_scaling:
|
||||
instances: 1
|
||||
@@ -0,0 +1,29 @@
|
||||
spec:
|
||||
template:
|
||||
spec:
|
||||
containers:
|
||||
- env:
|
||||
- name: SPRING_PROFILES_ACTIVE
|
||||
value: mysql
|
||||
- name: SPRING_DATASOURCE_USER
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: baeldung-db
|
||||
key: database-user
|
||||
- name: SPRING_DATASOURCE_PASSWORD
|
||||
valueFrom:
|
||||
secretKeyRef:
|
||||
name: baeldung-db
|
||||
key: database-password
|
||||
livenessProbe:
|
||||
httpGet:
|
||||
path: /actuator/health
|
||||
port: 8080
|
||||
scheme: HTTP
|
||||
initialDelaySeconds: 180
|
||||
readinessProbe:
|
||||
httpGet:
|
||||
path: /actuator/health
|
||||
port: 8080
|
||||
scheme: HTTP
|
||||
initialDelaySeconds: 20
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.autoconfigure.SpringBootApplication;
|
||||
import org.springframework.boot.autoconfigure.domain.EntityScan;
|
||||
import org.springframework.boot.web.servlet.ServletComponentScan;
|
||||
import org.springframework.data.jpa.repository.config.EnableJpaRepositories;
|
||||
|
||||
@ServletComponentScan
|
||||
@SpringBootApplication(scanBasePackages = "com.baeldung")
|
||||
@EnableJpaRepositories("com.baeldung.persistence.repo")
|
||||
@EntityScan("com.baeldung.persistence.model")
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.cloud.config;
|
||||
|
||||
import org.springframework.cloud.config.java.AbstractCloudConfig;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.context.annotation.Profile;
|
||||
|
||||
import javax.sql.DataSource;
|
||||
|
||||
@Configuration
|
||||
@Profile("cloud")
|
||||
public class CloudDataSourceConfig extends AbstractCloudConfig {
|
||||
|
||||
@Bean
|
||||
public DataSource dataSource() {
|
||||
return connectionFactory().dataSource();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.config;
|
||||
|
||||
import org.springframework.context.annotation.Configuration;
|
||||
import org.springframework.security.config.annotation.web.builders.HttpSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity;
|
||||
import org.springframework.security.config.annotation.web.configuration.WebSecurityConfigurerAdapter;
|
||||
|
||||
@Configuration
|
||||
@EnableWebSecurity
|
||||
public class SecurityConfig extends WebSecurityConfigurerAdapter {
|
||||
|
||||
@Override
|
||||
protected void configure(HttpSecurity http) throws Exception {
|
||||
http.authorizeRequests()
|
||||
.anyRequest()
|
||||
.permitAll()
|
||||
.and().csrf().disable();
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,95 @@
|
||||
package com.baeldung.persistence.model;
|
||||
|
||||
import javax.persistence.Column;
|
||||
import javax.persistence.Entity;
|
||||
import javax.persistence.GeneratedValue;
|
||||
import javax.persistence.GenerationType;
|
||||
import javax.persistence.Id;
|
||||
|
||||
@Entity
|
||||
public class Book {
|
||||
|
||||
@Id
|
||||
@GeneratedValue(strategy = GenerationType.AUTO)
|
||||
private long id;
|
||||
|
||||
@Column(nullable = false, unique = true)
|
||||
private String title;
|
||||
|
||||
@Column(nullable = false)
|
||||
private String author;
|
||||
|
||||
public Book() {
|
||||
super();
|
||||
}
|
||||
|
||||
public Book(String title, String author) {
|
||||
super();
|
||||
this.title = title;
|
||||
this.author = author;
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
|
||||
@Override
|
||||
public int hashCode() {
|
||||
final int prime = 31;
|
||||
int result = 1;
|
||||
result = prime * result + ((author == null) ? 0 : author.hashCode());
|
||||
result = prime * result + (int) (id ^ (id >>> 32));
|
||||
result = prime * result + ((title == null) ? 0 : title.hashCode());
|
||||
return result;
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean equals(Object obj) {
|
||||
if (this == obj)
|
||||
return true;
|
||||
if (obj == null)
|
||||
return false;
|
||||
if (getClass() != obj.getClass())
|
||||
return false;
|
||||
Book other = (Book) obj;
|
||||
if (author == null) {
|
||||
if (other.author != null)
|
||||
return false;
|
||||
} else if (!author.equals(other.author))
|
||||
return false;
|
||||
if (id != other.id)
|
||||
return false;
|
||||
if (title == null) {
|
||||
if (other.title != null)
|
||||
return false;
|
||||
} else if (!title.equals(other.title))
|
||||
return false;
|
||||
return true;
|
||||
}
|
||||
|
||||
@Override
|
||||
public String toString() {
|
||||
return "Book [id=" + id + ", title=" + title + ", author=" + author + "]";
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,12 @@
|
||||
package com.baeldung.persistence.repo;
|
||||
|
||||
import org.springframework.data.repository.CrudRepository;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
|
||||
import java.util.List;
|
||||
import java.util.Optional;
|
||||
|
||||
public interface BookRepository extends CrudRepository<Book, Long> {
|
||||
List<Book> findByTitle(String title);
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.springbootconfiguration;
|
||||
|
||||
import org.springframework.boot.SpringApplication;
|
||||
import org.springframework.boot.SpringBootConfiguration;
|
||||
import org.springframework.context.annotation.Bean;
|
||||
import org.springframework.context.annotation.ComponentScan;
|
||||
|
||||
@ComponentScan(basePackages = {"com.baeldung.springbootconfiguration.*"})
|
||||
@SpringBootConfiguration
|
||||
public class Application {
|
||||
|
||||
public static void main(String[] args) {
|
||||
SpringApplication.run(Application.class, args);
|
||||
}
|
||||
|
||||
@Bean
|
||||
public PersonService personService() {
|
||||
return new PersonServiceImpl();
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.springbootconfiguration;
|
||||
|
||||
public interface PersonService {
|
||||
}
|
||||
@@ -0,0 +1,4 @@
|
||||
package com.baeldung.springbootconfiguration;
|
||||
|
||||
public class PersonServiceImpl implements PersonService {
|
||||
}
|
||||
@@ -0,0 +1,69 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Autowired;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.web.bind.annotation.CrossOrigin;
|
||||
import org.springframework.web.bind.annotation.DeleteMapping;
|
||||
import org.springframework.web.bind.annotation.GetMapping;
|
||||
import org.springframework.web.bind.annotation.PathVariable;
|
||||
import org.springframework.web.bind.annotation.PostMapping;
|
||||
import org.springframework.web.bind.annotation.PutMapping;
|
||||
import org.springframework.web.bind.annotation.RequestBody;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
import org.springframework.web.bind.annotation.ResponseStatus;
|
||||
import org.springframework.web.bind.annotation.RestController;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
import com.baeldung.persistence.repo.BookRepository;
|
||||
import com.baeldung.web.exception.BookIdMismatchException;
|
||||
import com.baeldung.web.exception.BookNotFoundException;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
@RestController
|
||||
@RequestMapping("/api/books")
|
||||
public class BookController {
|
||||
|
||||
@Autowired
|
||||
private BookRepository bookRepository;
|
||||
|
||||
@GetMapping
|
||||
public Iterable<Book> findAll() {
|
||||
return bookRepository.findAll();
|
||||
}
|
||||
|
||||
@GetMapping("/title/{bookTitle}")
|
||||
public List<Book> findByTitle(@PathVariable String bookTitle) {
|
||||
return bookRepository.findByTitle(bookTitle);
|
||||
}
|
||||
|
||||
@GetMapping("/{id}")
|
||||
public Book findOne(@PathVariable long id) {
|
||||
return bookRepository.findById(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
}
|
||||
|
||||
@PostMapping
|
||||
@ResponseStatus(HttpStatus.CREATED)
|
||||
public Book create(@RequestBody Book book) {
|
||||
Book book1 = bookRepository.save(book);
|
||||
return book1;
|
||||
}
|
||||
|
||||
@DeleteMapping("/{id}")
|
||||
public void delete(@PathVariable long id) {
|
||||
bookRepository.findById(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
bookRepository.deleteById(id);
|
||||
}
|
||||
|
||||
@PutMapping("/{id}")
|
||||
public Book updateBook(@RequestBody Book book, @PathVariable long id) {
|
||||
if (book.getId() != id) {
|
||||
throw new BookIdMismatchException();
|
||||
}
|
||||
bookRepository.findById(id)
|
||||
.orElseThrow(BookNotFoundException::new);
|
||||
return bookRepository.save(book);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,37 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.hibernate.exception.ConstraintViolationException;
|
||||
import org.springframework.dao.DataIntegrityViolationException;
|
||||
import org.springframework.http.HttpHeaders;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.ResponseEntity;
|
||||
import org.springframework.web.bind.annotation.ControllerAdvice;
|
||||
import org.springframework.web.bind.annotation.ExceptionHandler;
|
||||
import org.springframework.web.context.request.WebRequest;
|
||||
import org.springframework.web.servlet.mvc.method.annotation.ResponseEntityExceptionHandler;
|
||||
|
||||
import com.baeldung.web.exception.BookIdMismatchException;
|
||||
import com.baeldung.web.exception.BookNotFoundException;
|
||||
|
||||
@ControllerAdvice
|
||||
public class RestExceptionHandler extends ResponseEntityExceptionHandler {
|
||||
|
||||
public RestExceptionHandler() {
|
||||
super();
|
||||
}
|
||||
|
||||
@ExceptionHandler(BookNotFoundException.class)
|
||||
protected ResponseEntity<Object> handleNotFound(Exception ex, WebRequest request) {
|
||||
return handleExceptionInternal(ex, "Book not found", new HttpHeaders(), HttpStatus.NOT_FOUND, request);
|
||||
}
|
||||
|
||||
@ExceptionHandler({
|
||||
BookIdMismatchException.class,
|
||||
ConstraintViolationException.class,
|
||||
DataIntegrityViolationException.class
|
||||
})
|
||||
public ResponseEntity<Object> handleBadRequest(Exception ex, WebRequest request) {
|
||||
return handleExceptionInternal(ex, ex
|
||||
.getLocalizedMessage(), new HttpHeaders(), HttpStatus.BAD_REQUEST, request);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,19 @@
|
||||
package com.baeldung.web;
|
||||
|
||||
import org.springframework.beans.factory.annotation.Value;
|
||||
import org.springframework.stereotype.Controller;
|
||||
import org.springframework.ui.Model;
|
||||
import org.springframework.web.bind.annotation.RequestMapping;
|
||||
|
||||
@Controller
|
||||
public class SimpleController {
|
||||
|
||||
@Value("${spring.application.name}")
|
||||
String appName;
|
||||
|
||||
@RequestMapping("/")
|
||||
public String homePage(Model model) {
|
||||
model.addAttribute("appName", appName);
|
||||
return "home";
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
public class BookIdMismatchException extends RuntimeException {
|
||||
|
||||
public BookIdMismatchException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BookIdMismatchException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public BookIdMismatchException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BookIdMismatchException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,20 @@
|
||||
package com.baeldung.web.exception;
|
||||
|
||||
public class BookNotFoundException extends RuntimeException {
|
||||
|
||||
public BookNotFoundException() {
|
||||
super();
|
||||
}
|
||||
|
||||
public BookNotFoundException(final String message, final Throwable cause) {
|
||||
super(message, cause);
|
||||
}
|
||||
|
||||
public BookNotFoundException(final String message) {
|
||||
super(message);
|
||||
}
|
||||
|
||||
public BookNotFoundException(final Throwable cause) {
|
||||
super(cause);
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.datasource.url=jdbc:mysql://${rds.hostname}:${rds.port}/${rds.db.name}
|
||||
spring.datasource.username=${rds.username}
|
||||
spring.datasource.password=${rds.password}
|
||||
@@ -0,0 +1,3 @@
|
||||
spring.cloud.gcp.sql.instance-connection-name=baeldung-spring-boot-bootstrap:europe-west2:baeldung-spring-boot-bootstrap-db
|
||||
spring.cloud.gcp.sql.database-name=baeldung_bootstrap_db
|
||||
spring.cloud.gcp.logging.enabled=true
|
||||
@@ -0,0 +1,20 @@
|
||||
server.port = 8081
|
||||
|
||||
spring.application.name = Bootstrap Spring Boot
|
||||
|
||||
spring.thymeleaf.cache = false
|
||||
spring.thymeleaf.enabled=true
|
||||
spring.thymeleaf.prefix=classpath:/templates/
|
||||
spring.thymeleaf.suffix=.html
|
||||
|
||||
spring.security.user.name=john
|
||||
spring.security.user.password=123
|
||||
|
||||
spring.datasource.driver-class-name=org.h2.Driver
|
||||
spring.datasource.url=jdbc:h2:mem:bootapp;DB_CLOSE_DELAY=-1;DB_CLOSE_ON_EXIT=FALSE
|
||||
spring.datasource.username=sa
|
||||
spring.datasource.password=
|
||||
spring.jpa.database-platform=org.hibernate.dialect.H2Dialect
|
||||
|
||||
server.error.path=/error
|
||||
server.error.whitelabel.enabled=false
|
||||
@@ -0,0 +1 @@
|
||||
spring.jpa.database-platform=org.hibernate.dialect.MySQL5InnoDBDialect
|
||||
@@ -0,0 +1,12 @@
|
||||
server.port=${port:8080}
|
||||
spring.application.name = Bootstrap Spring Cloud
|
||||
spring.thymeleaf.cache = false
|
||||
spring.thymeleaf.enabled=true
|
||||
spring.thymeleaf.prefix=classpath:/templates/
|
||||
spring.thymeleaf.suffix=.html
|
||||
|
||||
server.error.path=/error
|
||||
server.error.whitelabel.enabled=false
|
||||
|
||||
spring.jpa.generate-ddl=true
|
||||
spring.jpa.hibernate.ddl-auto=update
|
||||
@@ -0,0 +1,12 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<include resource="org/springframework/cloud/gcp/autoconfigure/logging/logback-appender.xml" />
|
||||
<include resource="org/springframework/boot/logging/logback/defaults.xml"/>
|
||||
<include resource="org/springframework/boot/logging/logback/console-appender.xml" />
|
||||
|
||||
<root level="INFO">
|
||||
<!-- If running in GCP, remove the CONSOLE appender otherwise logs will be duplicated. -->
|
||||
<appender-ref ref="CONSOLE"/>
|
||||
<appender-ref ref="STACKDRIVER" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1,13 @@
|
||||
<?xml version="1.0" encoding="UTF-8"?>
|
||||
<configuration>
|
||||
<appender name="STDOUT" class="ch.qos.logback.core.ConsoleAppender">
|
||||
<encoder>
|
||||
<pattern>%d{HH:mm:ss.SSS} [%thread] %-5level %logger{36} - %msg%n
|
||||
</pattern>
|
||||
</encoder>
|
||||
</appender>
|
||||
|
||||
<root level="INFO">
|
||||
<appender-ref ref="STDOUT" />
|
||||
</root>
|
||||
</configuration>
|
||||
@@ -0,0 +1 @@
|
||||
spring.cloud.appId=baeldung-spring-boot-bootstrap
|
||||
@@ -0,0 +1,19 @@
|
||||
<html lang="en" xmlns:th="http://www.w3.org/1999/xhtml">
|
||||
<head>
|
||||
<title>Error Occurred</title>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" />
|
||||
</head>
|
||||
<body>
|
||||
<div class="container">
|
||||
<h1>Error Occurred!</h1>
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<b>
|
||||
[<span th:text="${status}">status</span>]
|
||||
<span th:text="${error}">error</span>
|
||||
</b>
|
||||
<p th:text="${message}">message</p>
|
||||
</div>
|
||||
</div>
|
||||
</body>
|
||||
|
||||
</html>
|
||||
@@ -0,0 +1,7 @@
|
||||
<html xmlns:th="http://www.w3.org/1999/xhtml" lang="en">
|
||||
<head><title>Home Page</title></head>
|
||||
<body>
|
||||
<h1>Hello !</h1>
|
||||
<p>Welcome to <span th:text="${appName}">Our App</span></p>
|
||||
</body>
|
||||
</html>
|
||||
@@ -0,0 +1,131 @@
|
||||
package com.baeldung;
|
||||
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomAlphabetic;
|
||||
import static org.apache.commons.lang3.RandomStringUtils.randomNumeric;
|
||||
import static org.junit.Assert.assertEquals;
|
||||
import static org.junit.Assert.assertTrue;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.springframework.http.HttpStatus;
|
||||
import org.springframework.http.MediaType;
|
||||
|
||||
import com.baeldung.persistence.model.Book;
|
||||
|
||||
import io.restassured.RestAssured;
|
||||
import io.restassured.response.Response;
|
||||
|
||||
public class SpringBootBootstrapLiveTest {
|
||||
|
||||
private static final String API_ROOT = "http://localhost:8080/api/books";
|
||||
|
||||
@Test
|
||||
public void whenGetAllBooks_thenOK() {
|
||||
final Response response = RestAssured.get(API_ROOT);
|
||||
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetBooksByTitle_thenOK() {
|
||||
final Book book = createRandomBook();
|
||||
createBookAsUri(book);
|
||||
|
||||
final Response response = RestAssured.get(API_ROOT + "/title/" + book.getTitle());
|
||||
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
|
||||
assertTrue(response.as(List.class)
|
||||
.size() > 0);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetCreatedBookById_thenOK() {
|
||||
final Book book = createRandomBook();
|
||||
final String location = createBookAsUri(book);
|
||||
|
||||
final Response response = RestAssured.get(location);
|
||||
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
|
||||
assertEquals(book.getTitle(), response.jsonPath()
|
||||
.get("title"));
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenGetNotExistBookById_thenNotFound() {
|
||||
final Response response = RestAssured.get(API_ROOT + "/" + randomNumeric(4));
|
||||
assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode());
|
||||
}
|
||||
|
||||
// POST
|
||||
@Test
|
||||
public void whenCreateNewBook_thenCreated() {
|
||||
final Book book = createRandomBook();
|
||||
|
||||
final Response response = RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(book)
|
||||
.post(API_ROOT);
|
||||
assertEquals(HttpStatus.CREATED.value(), response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenInvalidBook_thenError() {
|
||||
final Book book = createRandomBook();
|
||||
book.setAuthor(null);
|
||||
|
||||
final Response response = RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(book)
|
||||
.post(API_ROOT);
|
||||
assertEquals(HttpStatus.BAD_REQUEST.value(), response.getStatusCode());
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenUpdateCreatedBook_thenUpdated() {
|
||||
final Book book = createRandomBook();
|
||||
final String location = createBookAsUri(book);
|
||||
|
||||
book.setId(Long.parseLong(location.split("api/books/")[1]));
|
||||
book.setAuthor("newAuthor");
|
||||
Response response = RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(book)
|
||||
.put(location);
|
||||
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
|
||||
|
||||
response = RestAssured.get(location);
|
||||
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
|
||||
assertEquals("newAuthor", response.jsonPath()
|
||||
.get("author"));
|
||||
|
||||
}
|
||||
|
||||
@Test
|
||||
public void whenDeleteCreatedBook_thenOk() {
|
||||
final Book book = createRandomBook();
|
||||
final String location = createBookAsUri(book);
|
||||
|
||||
Response response = RestAssured.delete(location);
|
||||
assertEquals(HttpStatus.OK.value(), response.getStatusCode());
|
||||
|
||||
response = RestAssured.get(location);
|
||||
assertEquals(HttpStatus.NOT_FOUND.value(), response.getStatusCode());
|
||||
}
|
||||
|
||||
// ===============================
|
||||
|
||||
private Book createRandomBook() {
|
||||
final Book book = new Book();
|
||||
book.setTitle(randomAlphabetic(10));
|
||||
book.setAuthor(randomAlphabetic(15));
|
||||
return book;
|
||||
}
|
||||
|
||||
private String createBookAsUri(Book book) {
|
||||
final Response response = RestAssured.given()
|
||||
.contentType(MediaType.APPLICATION_JSON_VALUE)
|
||||
.body(book)
|
||||
.post(API_ROOT);
|
||||
return API_ROOT + "/" + response.jsonPath()
|
||||
.get("id");
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,15 @@
|
||||
package com.baeldung;
|
||||
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.springframework.boot.test.context.SpringBootTest;
|
||||
import org.springframework.test.context.junit4.SpringRunner;
|
||||
|
||||
@RunWith(SpringRunner.class)
|
||||
@SpringBootTest
|
||||
public class SpringContextTest {
|
||||
|
||||
@Test
|
||||
public void contextLoads() {
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user