diff --git a/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java b/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java
index 336b2ac324..278d69d4ac 100644
--- a/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java
+++ b/core-java-modules/core-java-string-conversions/src/test/java/com/baeldung/stringtoint/StringToIntOrIntegerUnitTest.java
@@ -17,6 +17,15 @@ public class StringToIntOrIntegerUnitTest {
assertThat(result).isEqualTo(42);
}
+ @Test
+ public void givenBinaryString_whenParsingInt_shouldConvertToInt() {
+ String givenString = "101010";
+
+ int result = Integer.parseInt(givenString, 2);
+
+ assertThat(result).isEqualTo(42);
+ }
+
@Test
public void givenString_whenCallingIntegerValueOf_shouldConvertToInt() {
String givenString = "42";
@@ -27,6 +36,15 @@ public class StringToIntOrIntegerUnitTest {
}
@Test
+ public void givenBinaryString_whenCallingIntegerValueOf_shouldConvertToInt() {
+ String givenString = "101010";
+
+ Integer result = Integer.valueOf(givenString, 2);
+
+ assertThat(result).isEqualTo(new Integer(42));
+ }
+
+ @Test
public void givenString_whenCallingValueOf_shouldCacheSomeValues() {
for (int i = -128; i <= 127; i++) {
String value = i + "";
diff --git a/guava-collections-map/pom.xml b/guava-collections-map/pom.xml
index ee8ceb10f3..06537d26bd 100644
--- a/guava-collections-map/pom.xml
+++ b/guava-collections-map/pom.xml
@@ -16,12 +16,39 @@
guava-collections-map
+
src/main/resources
true
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.2
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit-jupiter.version}
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+ ${junit-jupiter.version}
+ test
+
+
+
+
+ 5.6.2
+
\ No newline at end of file
diff --git a/guava-collections-set/pom.xml b/guava-collections-set/pom.xml
index 8bb0b924f9..49d96965a7 100644
--- a/guava-collections-set/pom.xml
+++ b/guava-collections-set/pom.xml
@@ -13,8 +13,32 @@
../parent-java
+
+ guava-collections-set
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.2
+
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit-jupiter.version}
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+ ${junit-jupiter.version}
+ test
+
org.assertj
assertj-core
@@ -23,13 +47,10 @@
-
- guava-collections-set
-
-
3.6.1
+ 5.6.2
diff --git a/guava-collections/pom.xml b/guava-collections/pom.xml
index c6019362c5..744eba1a38 100644
--- a/guava-collections/pom.xml
+++ b/guava-collections/pom.xml
@@ -15,6 +15,25 @@
../parent-java
+
+ guava-collections
+
+
+
+ src/main/resources
+ true
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.2
+
+
+
+
@@ -27,7 +46,20 @@
commons-lang3
${commons-lang3.version}
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit-jupiter.version}
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+ ${junit-jupiter.version}
+ test
+
org.assertj
assertj-core
@@ -44,16 +76,6 @@
-
- guava-collections
-
-
- src/main/resources
- true
-
-
-
-
4.1
@@ -61,6 +83,7 @@
3.6.1
2.0.0.0
+ 5.6.2
\ No newline at end of file
diff --git a/guava-io/pom.xml b/guava-io/pom.xml
index 7517d442b0..fd637f2474 100644
--- a/guava-io/pom.xml
+++ b/guava-io/pom.xml
@@ -4,6 +4,9 @@
4.0.0
guava-io
0.1.0-SNAPSHOT
+
+ 5.6.2
+
guava-io
@@ -15,12 +18,35 @@
guava-io
+
src/main/resources
true
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.2
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit-jupiter.version}
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+ ${junit-jupiter.version}
+ test
+
+
\ No newline at end of file
diff --git a/guava-io/src/main/test/java/com/baeldung/guava/GuavaCountingOutputStreamUnitTest.java b/guava-io/src/test/java/com/baeldung/guava/GuavaCountingOutputStreamUnitTest.java
similarity index 100%
rename from guava-io/src/main/test/java/com/baeldung/guava/GuavaCountingOutputStreamUnitTest.java
rename to guava-io/src/test/java/com/baeldung/guava/GuavaCountingOutputStreamUnitTest.java
diff --git a/guava-io/src/main/test/java/com/baeldung/guava/GuavaIOUnitTest.java b/guava-io/src/test/java/com/baeldung/guava/GuavaIOUnitTest.java
similarity index 93%
rename from guava-io/src/main/test/java/com/baeldung/guava/GuavaIOUnitTest.java
rename to guava-io/src/test/java/com/baeldung/guava/GuavaIOUnitTest.java
index 7d7b0ea04d..4d7c688f58 100644
--- a/guava-io/src/main/test/java/com/baeldung/guava/GuavaIOUnitTest.java
+++ b/guava-io/src/test/java/com/baeldung/guava/GuavaIOUnitTest.java
@@ -11,8 +11,11 @@ import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import java.net.URL;
+import java.nio.file.Path;
+import java.nio.file.Paths;
import java.util.List;
+import org.junit.After;
import org.junit.Test;
import com.google.common.base.Charsets;
@@ -31,6 +34,21 @@ import com.google.common.io.Resources;
public class GuavaIOUnitTest {
+ @After
+ public void afterEach() throws Exception {
+ deleteProducedFiles();
+ }
+
+ private void deleteProducedFiles() throws IOException {
+ deleteIfExists("test.out");
+ deleteIfExists("test_copy.in");
+ deleteIfExists("test_le.txt");
+ }
+
+ private void deleteIfExists(String fileName) throws IOException {
+ java.nio.file.Files.deleteIfExists(Paths.get("src", "test", "resources", fileName));
+ }
+
@Test
public void whenWriteUsingFiles_thenWritten() throws IOException {
final String expectedValue = "Hello world";
@@ -206,5 +224,4 @@ public class GuavaIOUnitTest {
assertEquals(value, result);
}
-
}
diff --git a/guava-io/src/test/resources/test1.in b/guava-io/src/test/resources/test1.in
new file mode 100644
index 0000000000..70c379b63f
--- /dev/null
+++ b/guava-io/src/test/resources/test1.in
@@ -0,0 +1 @@
+Hello world
\ No newline at end of file
diff --git a/guava-io/src/test/resources/test1_1.in b/guava-io/src/test/resources/test1_1.in
new file mode 100644
index 0000000000..8318c86b35
--- /dev/null
+++ b/guava-io/src/test/resources/test1_1.in
@@ -0,0 +1 @@
+Test
\ No newline at end of file
diff --git a/guava-io/src/test/resources/test2.in b/guava-io/src/test/resources/test2.in
new file mode 100644
index 0000000000..622efea9e6
--- /dev/null
+++ b/guava-io/src/test/resources/test2.in
@@ -0,0 +1,4 @@
+John
+Jane
+Adam
+Tom
\ No newline at end of file
diff --git a/guava-modules/guava-18/pom.xml b/guava-modules/guava-18/pom.xml
index 30d9f953ac..d65fab1e57 100644
--- a/guava-modules/guava-18/pom.xml
+++ b/guava-modules/guava-18/pom.xml
@@ -8,9 +8,9 @@
com.baeldung
- parent-java
+ guava-modules
0.0.1-SNAPSHOT
- ../../parent-java
+ ../
diff --git a/guava-modules/guava-19/pom.xml b/guava-modules/guava-19/pom.xml
index 0060afd426..20a405cff4 100644
--- a/guava-modules/guava-19/pom.xml
+++ b/guava-modules/guava-19/pom.xml
@@ -8,9 +8,9 @@
com.baeldung
- parent-java
+ guava-modules
0.0.1-SNAPSHOT
- ../../parent-java
+ ../
diff --git a/guava-modules/guava-21/pom.xml b/guava-modules/guava-21/pom.xml
index 7932cfa6d8..b126df99cb 100644
--- a/guava-modules/guava-21/pom.xml
+++ b/guava-modules/guava-21/pom.xml
@@ -8,9 +8,9 @@
com.baeldung
- parent-java
+ guava-modules
0.0.1-SNAPSHOT
- ../../parent-java
+ ../
diff --git a/guava-modules/pom.xml b/guava-modules/pom.xml
index 2b899df162..4e7282364d 100644
--- a/guava-modules/pom.xml
+++ b/guava-modules/pom.xml
@@ -4,13 +4,16 @@
4.0.0
guava-modules
guava-modules
+
+ 5.6.2
+
pom
com.baeldung
- parent-modules
- 1.0.0-SNAPSHOT
- ..
+ parent-java
+ 0.0.1-SNAPSHOT
+ ../parent-java
@@ -19,4 +22,28 @@
guava-21
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.2
+
+
+
+
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit-jupiter.version}
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+ ${junit-jupiter.version}
+ test
+
+
diff --git a/guava/pom.xml b/guava/pom.xml
index df6d57bd09..881390ae73 100644
--- a/guava/pom.xml
+++ b/guava/pom.xml
@@ -15,13 +15,45 @@
../parent-java
+
+ guava
+
+
+
+ src/main/resources
+ true
+
+
+
+
+
+ org.apache.maven.plugins
+ maven-surefire-plugin
+ 2.22.2
+
+
+
+
org.apache.commons
commons-lang3
${commons-lang3.version}
+
+
+ org.junit.jupiter
+ junit-jupiter
+ ${junit-jupiter.version}
+ test
+
+
+ org.junit.vintage
+ junit-vintage-engine
+ ${junit-jupiter.version}
+ test
+
org.assertj
assertj-core
@@ -30,18 +62,9 @@
-
- guava
-
-
- src/main/resources
- true
-
-
-
-
+ 5.6.2
3.6.1
diff --git a/persistence-modules/core-java-persistence/pom.xml b/persistence-modules/core-java-persistence/pom.xml
index 1224523ac7..3dd8da1b7a 100644
--- a/persistence-modules/core-java-persistence/pom.xml
+++ b/persistence-modules/core-java-persistence/pom.xml
@@ -60,6 +60,7 @@
+ 1.4.200
42.2.5.jre7
3.10.0
2.4.0
diff --git a/persistence-modules/core-java-persistence/src/test/java/com/baeldung/genkeys/JdbcInsertIdIntegrationTest.java b/persistence-modules/core-java-persistence/src/test/java/com/baeldung/genkeys/JdbcInsertIdIntegrationTest.java
new file mode 100644
index 0000000000..f1ea5bfbb4
--- /dev/null
+++ b/persistence-modules/core-java-persistence/src/test/java/com/baeldung/genkeys/JdbcInsertIdIntegrationTest.java
@@ -0,0 +1,93 @@
+package com.baeldung.genkeys;
+
+import org.junit.AfterClass;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+import java.sql.Connection;
+import java.sql.DriverManager;
+import java.sql.PreparedStatement;
+import java.sql.ResultSet;
+import java.sql.SQLException;
+import java.sql.Statement;
+
+import static org.assertj.core.api.Assertions.assertThat;
+
+public class JdbcInsertIdIntegrationTest {
+
+ private static final String QUERY = "insert into persons (name) values (?)";
+
+ private static Connection connection;
+
+ @BeforeClass
+ public static void setUp() throws Exception {
+ connection = DriverManager.getConnection("jdbc:h2:mem:generated-keys", "sa", "");
+ connection
+ .createStatement()
+ .execute("create table persons(id bigint auto_increment, name varchar(255))");
+ }
+
+ @AfterClass
+ public static void tearDown() throws SQLException {
+ connection
+ .createStatement()
+ .execute("drop table persons");
+ connection.close();
+ }
+
+ @Test
+ public void givenInsert_whenUsingAutoGenFlag_thenBeAbleToFetchTheIdAfterward() throws SQLException {
+ try (PreparedStatement statement = connection.prepareStatement(QUERY, Statement.RETURN_GENERATED_KEYS)) {
+ statement.setString(1, "Foo");
+ int affectedRows = statement.executeUpdate();
+ assertThat(affectedRows).isPositive();
+
+ try (ResultSet keys = statement.getGeneratedKeys()) {
+ assertThat(keys.next()).isTrue();
+ assertThat(keys.getLong(1)).isGreaterThanOrEqualTo(1);
+ }
+ }
+ }
+
+ @Test
+ public void givenInsert_whenUsingAutoGenFlagViaExecute_thenBeAbleToFetchTheIdAfterward() throws SQLException {
+ try (Statement statement = connection.createStatement()) {
+ String query = "insert into persons (name) values ('Foo')";
+ int affectedRows = statement.executeUpdate(query, Statement.RETURN_GENERATED_KEYS);
+ assertThat(affectedRows).isPositive();
+
+ try (ResultSet keys = statement.getGeneratedKeys()) {
+ assertThat(keys.next()).isTrue();
+ assertThat(keys.getLong(1)).isGreaterThanOrEqualTo(1);
+ }
+ }
+ }
+
+ @Test
+ public void givenInsert_whenUsingReturningCols_thenBeAbleToFetchTheIdAfterward() throws SQLException {
+ try (PreparedStatement statement = connection.prepareStatement(QUERY, new String[] { "id" })) {
+ statement.setString(1, "Foo");
+ int affectedRows = statement.executeUpdate();
+ assertThat(affectedRows).isPositive();
+
+ try (ResultSet keys = statement.getGeneratedKeys()) {
+ assertThat(keys.next()).isTrue();
+ assertThat(keys.getLong(1)).isGreaterThanOrEqualTo(1);
+ }
+ }
+ }
+
+ @Test
+ public void givenInsert_whenUsingReturningColsViaExecute_thenBeAbleToFetchTheIdAfterward() throws SQLException {
+ try (Statement statement = connection.createStatement()) {
+ String query = "insert into persons (name) values ('Foo')";
+ int affectedRows = statement.executeUpdate(query, new String[] { "id" });
+ assertThat(affectedRows).isPositive();
+
+ try (ResultSet keys = statement.getGeneratedKeys()) {
+ assertThat(keys.next()).isTrue();
+ assertThat(keys.getLong(1)).isGreaterThanOrEqualTo(1);
+ }
+ }
+ }
+}
diff --git a/persistence-modules/java-jpa-2/README.md b/persistence-modules/java-jpa-2/README.md
index 4b822c4782..d711eef1b0 100644
--- a/persistence-modules/java-jpa-2/README.md
+++ b/persistence-modules/java-jpa-2/README.md
@@ -13,4 +13,5 @@ This module contains articles about the Java Persistence API (JPA) in Java.
- [JPA Annotation for the PostgreSQL TEXT Type](https://www.baeldung.com/jpa-annotation-postgresql-text-type)
- [Mapping a Single Entity to Multiple Tables in JPA](https://www.baeldung.com/jpa-mapping-single-entity-to-multiple-tables)
- [Constructing a JPA Query Between Unrelated Entities](https://www.baeldung.com/jpa-query-unrelated-entities)
+- [When Does JPA Set the Primary Key](https://www.baeldung.com/jpa-strategies-when-set-primary-key)
- More articles: [[<-- prev]](/java-jpa)
diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Admin.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Admin.java
new file mode 100644
index 0000000000..1c59b33ab8
--- /dev/null
+++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Admin.java
@@ -0,0 +1,36 @@
+package com.baeldung.jpa.generateidvalue;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "app_admin")
+public class Admin {
+
+ @Id
+ @GeneratedValue
+ private Long id;
+
+ @Column(name = "admin_name")
+ private String name;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Article.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Article.java
new file mode 100644
index 0000000000..06465de179
--- /dev/null
+++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Article.java
@@ -0,0 +1,39 @@
+package com.baeldung.jpa.generateidvalue;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.SequenceGenerator;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "article")
+public class Article {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.SEQUENCE, generator = "article_gen")
+ @SequenceGenerator(name = "article_gen", sequenceName = "article_seq")
+ private Long id;
+
+ @Column(name = "article_name")
+ private String name;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/IdGenerator.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/IdGenerator.java
new file mode 100644
index 0000000000..0fac86747f
--- /dev/null
+++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/IdGenerator.java
@@ -0,0 +1,48 @@
+package com.baeldung.jpa.generateidvalue;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Table(name = "id_gen")
+@Entity
+public class IdGenerator {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @Column(name = "gen_name")
+ private String gen_name;
+
+ @Column(name = "gen_value")
+ private Long gen_value;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getGen_name() {
+ return gen_name;
+ }
+
+ public void setGen_name(String gen_name) {
+ this.gen_name = gen_name;
+ }
+
+ public Long getGen_value() {
+ return gen_value;
+ }
+
+ public void setGen_value(Long gen_value) {
+ this.gen_value = gen_value;
+ }
+
+}
\ No newline at end of file
diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Task.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Task.java
new file mode 100644
index 0000000000..a51ec53418
--- /dev/null
+++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/Task.java
@@ -0,0 +1,39 @@
+package com.baeldung.jpa.generateidvalue;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+import javax.persistence.TableGenerator;
+
+@Entity
+@Table(name = "task")
+public class Task {
+
+ @Id
+ @TableGenerator(name = "id_generator", table = "id_gen", pkColumnName = "gen_name", valueColumnName = "gen_value", pkColumnValue = "task_gen", initialValue = 10000, allocationSize = 10)
+ @GeneratedValue(strategy = GenerationType.TABLE, generator = "id_generator")
+ private Long id;
+
+ @Column(name = "name")
+ private String name;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/User.java b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/User.java
new file mode 100644
index 0000000000..88fefe7ba6
--- /dev/null
+++ b/persistence-modules/java-jpa-2/src/main/java/com/baeldung/jpa/generateidvalue/User.java
@@ -0,0 +1,37 @@
+package com.baeldung.jpa.generateidvalue;
+
+import javax.persistence.Column;
+import javax.persistence.Entity;
+import javax.persistence.GeneratedValue;
+import javax.persistence.GenerationType;
+import javax.persistence.Id;
+import javax.persistence.Table;
+
+@Entity
+@Table(name = "app_user")
+public class User {
+
+ @Id
+ @GeneratedValue(strategy = GenerationType.IDENTITY)
+ private Long id;
+
+ @Column(name = "user_name")
+ private String name;
+
+ public Long getId() {
+ return id;
+ }
+
+ public void setId(Long id) {
+ this.id = id;
+ }
+
+ public String getName() {
+ return name;
+ }
+
+ public void setName(String name) {
+ this.name = name;
+ }
+
+}
\ No newline at end of file
diff --git a/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml b/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml
index eec7f7cf6e..3bc81910d9 100644
--- a/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml
+++ b/persistence-modules/java-jpa-2/src/main/resources/META-INF/persistence.xml
@@ -184,4 +184,29 @@
value="false" />
+
+
+ org.eclipse.persistence.jpa.PersistenceProvider
+ com.baeldung.jpa.generateidvalue.Admin
+ com.baeldung.jpa.generateidvalue.Article
+ com.baeldung.jpa.generateidvalue.IdGenerator
+ com.baeldung.jpa.generateidvalue.Task
+ com.baeldung.jpa.generateidvalue.User
+ true
+
+
+
+
+
+
+
+
+
+
+
+
+
diff --git a/persistence-modules/java-jpa-2/src/main/resources/primary_key_generator.sql b/persistence-modules/java-jpa-2/src/main/resources/primary_key_generator.sql
new file mode 100644
index 0000000000..9acd1bc11b
--- /dev/null
+++ b/persistence-modules/java-jpa-2/src/main/resources/primary_key_generator.sql
@@ -0,0 +1,4 @@
+CREATE SEQUENCE article_seq MINVALUE 1 START WITH 50 INCREMENT BY 50;
+
+INSERT INTO id_gen (gen_name, gen_val) VALUES ('id_generator', 0);
+INSERT INTO id_gen (gen_name, gen_val) VALUES ('task_gen', 10000);
\ No newline at end of file
diff --git a/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/generateidvalue/PrimaryKeyUnitTest.java b/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/generateidvalue/PrimaryKeyUnitTest.java
new file mode 100644
index 0000000000..eead56dbff
--- /dev/null
+++ b/persistence-modules/java-jpa-2/src/test/java/com/baeldung/jpa/generateidvalue/PrimaryKeyUnitTest.java
@@ -0,0 +1,81 @@
+package com.baeldung.jpa.generateidvalue;
+
+import javax.persistence.EntityManager;
+import javax.persistence.EntityManagerFactory;
+import javax.persistence.Persistence;
+
+import org.junit.Assert;
+import org.junit.BeforeClass;
+import org.junit.Test;
+
+/**
+ * PrimaryKeyGeneratorTest class
+ *
+ * @author shiwangzhihe@gmail.com
+ */
+public class PrimaryKeyUnitTest {
+ private static EntityManager entityManager;
+
+ @BeforeClass
+ public static void setup() {
+ EntityManagerFactory factory = Persistence.createEntityManagerFactory("jpa-h2-primarykey");
+ entityManager = factory.createEntityManager();
+ }
+
+ @Test
+ public void givenIdentityStrategy_whenCommitTransction_thenReturnPrimaryKey() {
+ User user = new User();
+ user.setName("TestName");
+
+ entityManager.getTransaction()
+ .begin();
+ entityManager.persist(user);
+ Assert.assertNull(user.getId());
+ entityManager.getTransaction()
+ .commit();
+
+ Long expectPrimaryKey = 1L;
+ Assert.assertEquals(expectPrimaryKey, user.getId());
+ }
+
+ @Test
+ public void givenTableStrategy_whenPersist_thenReturnPrimaryKey() {
+ Task task = new Task();
+ task.setName("Test Task");
+
+ entityManager.getTransaction()
+ .begin();
+ entityManager.persist(task);
+ Long expectPrimaryKey = 10000L;
+ Assert.assertEquals(expectPrimaryKey, task.getId());
+
+ entityManager.getTransaction()
+ .commit();
+ }
+
+ @Test
+ public void givenSequenceStrategy_whenPersist_thenReturnPrimaryKey() {
+ Article article = new Article();
+ article.setName("Test Name");
+
+ entityManager.getTransaction()
+ .begin();
+ entityManager.persist(article);
+ Long expectPrimaryKey = 51L;
+ Assert.assertEquals(expectPrimaryKey, article.getId());
+
+ entityManager.getTransaction()
+ .commit();
+ }
+
+ @Test
+ public void givenAutoStrategy_whenPersist_thenReturnPrimaryKey() {
+ Admin admin = new Admin();
+ admin.setName("Test Name");
+
+ entityManager.persist(admin);
+
+ Long expectPrimaryKey = 1L;
+ Assert.assertEquals(expectPrimaryKey, admin.getId());
+ }
+}
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-properties/.dockerignore b/spring-boot-modules/spring-boot-properties/.dockerignore
deleted file mode 100644
index df36044e46..0000000000
--- a/spring-boot-modules/spring-boot-properties/.dockerignore
+++ /dev/null
@@ -1,13 +0,0 @@
-# Logs
-logs
-*.log
-
-# Git
-.git
-.cache
-
-# Classes
-**/*.class
-
-# Ignore md files
-*.md
diff --git a/spring-boot-modules/spring-boot-properties/Dockerfile b/spring-boot-modules/spring-boot-properties/Dockerfile
deleted file mode 100644
index d6bd2a95ae..0000000000
--- a/spring-boot-modules/spring-boot-properties/Dockerfile
+++ /dev/null
@@ -1,10 +0,0 @@
-FROM maven:3.6.0-jdk-11
-WORKDIR /code/spring-boot-modules/spring-boot-properties/
-COPY ./spring-boot-modules/spring-boot-properties/pom.xml .
-COPY ./spring-boot-modules/spring-boot-properties/src ./src
-COPY ./parent-boot-2/pom.xml /code/parent-boot-2/pom.xml
-COPY ./pom.xml /code/pom.xml
-COPY ./custom-pmd-0.0.1.jar /code/custom-pmd-0.0.1.jar
-COPY ./baeldung-pmd-rules.xml /code/baeldung-pmd-rules.xml
-RUN mvn dependency:resolve
-CMD ["mvn", "spring-boot:run"]
\ No newline at end of file
diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java
index d42b731213..f3cfff57b7 100644
--- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java
+++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/MyApplication.java
@@ -11,6 +11,9 @@ import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
+import com.baeldung.yaml.YAMLConfig.Idm;
+import com.baeldung.yaml.YAMLConfig.Service;
+
@SpringBootApplication
public class MyApplication implements CommandLineRunner {
@@ -26,6 +29,23 @@ public class MyApplication implements CommandLineRunner {
System.out.println("using environment:" + myConfig.getEnvironment());
System.out.println("name:" + myConfig.getName());
System.out.println("servers:" + myConfig.getServers());
+
+ if ("testing".equalsIgnoreCase(myConfig.getEnvironment())) {
+ System.out.println("external:" + myConfig.getExternal());
+ System.out.println("map:" + myConfig.getMap());
+
+ Idm idm = myConfig.getComponent().getIdm();
+ Service service = myConfig.getComponent().getService();
+ System.out.println("Idm:");
+ System.out.println(" Url: " + idm.getUrl());
+ System.out.println(" User: " + idm.getUser());
+ System.out.println(" Password: " + idm.getPassword());
+ System.out.println(" Description: " + idm.getDescription());
+ System.out.println("Service:");
+ System.out.println(" Url: " + service.getUrl());
+ System.out.println(" Token: " + service.getToken());
+ System.out.println(" Description: " + service.getDescription());
+ }
}
}
diff --git a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java
index ad633c4b56..83c083734c 100644
--- a/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java
+++ b/spring-boot-modules/spring-boot-properties/src/main/java/com/baeldung/yaml/YAMLConfig.java
@@ -1,7 +1,10 @@
package com.baeldung.yaml;
import java.util.ArrayList;
+import java.util.HashMap;
import java.util.List;
+import java.util.Map;
+
import org.springframework.boot.context.properties.ConfigurationProperties;
import org.springframework.boot.context.properties.EnableConfigurationProperties;
import org.springframework.context.annotation.Configuration;
@@ -13,6 +16,9 @@ public class YAMLConfig {
private String name;
private String environment;
private List servers = new ArrayList();
+ private List external = new ArrayList();
+ private Map map = new HashMap();
+ private Component component = new Component();
public List getServers() {
return servers;
@@ -37,5 +43,111 @@ public class YAMLConfig {
public void setEnvironment(String environment) {
this.environment = environment;
}
+
+ public Component getComponent() {
+ return component;
+ }
+
+ public void setComponent(Component component) {
+ this.component = component;
+ }
+
+ public Map getMap() {
+ return map;
+ }
+
+ public void setMap(Map map) {
+ this.map = map;
+ }
+
+ public List getExternal() {
+ return external;
+ }
+
+ public void setExternal(List external) {
+ this.external = external;
+ }
+
+ public class Component {
+ private Idm idm = new Idm();
+ private Service service = new Service();
+
+ public Idm getIdm() {
+ return idm;
+ }
+ public void setIdm(Idm idm) {
+ this.idm = idm;
+ }
+
+ public Service getService() {
+ return service;
+ }
+ public void setService(Service service) {
+ this.service = service;
+ }
+
+ }
+
+ public class Idm {
+ private String url;
+ private String user;
+ private String password;
+ private String description;
+
+ public String getUrl() {
+ return url;
+ }
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public String getUser() {
+ return user;
+ }
+ public void setUser(String user) {
+ this.user = user;
+ }
+
+ public String getPassword() {
+ return password;
+ }
+ public void setPassword(String password) {
+ this.password = password;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+ public void setDescription(String description) {
+ this.description = description;
+ }
+ }
+
+ public class Service {
+ private String url;
+ private String token;
+ private String description;
+
+ public String getUrl() {
+ return url;
+ }
+ public void setUrl(String url) {
+ this.url = url;
+ }
+
+ public String getToken() {
+ return token;
+ }
+ public void setToken(String token) {
+ this.token = token;
+ }
+
+ public String getDescription() {
+ return description;
+ }
+ public void setDescription(String description) {
+ this.description = description;
+ }
+ }
}
diff --git a/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml b/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml
index 4914ff15f7..30e64f9d35 100644
--- a/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml
+++ b/spring-boot-modules/spring-boot-properties/src/main/resources/application.yml
@@ -6,22 +6,42 @@ spring:
---
spring:
- profiles: test
+ profiles: test
name: test-YAML
environment: testing
servers:
- - www.abc.test.com
- - www.xyz.test.com
-
+ - www.abc.test.com
+ - www.xyz.test.com
+
+external: [www.abc.test.com, www.xyz.test.com]
+
+map:
+ firstkey: key1
+ secondkey: key2
+
+component:
+ idm:
+ url: myurl
+ user: user
+ password: password
+ description: >
+ this should be a long
+ description
+ service:
+ url: myurlservice
+ token: token
+ description: >
+ this should be another long
+ description
---
spring:
- profiles: prod
+ profiles: prod
name: prod-YAML
environment: production
servers:
- - www.abc.com
- - www.xyz.com
+ - www.abc.com
+ - www.xyz.com
---
diff --git a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java
index 090d5c592e..19412c91f5 100644
--- a/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java
+++ b/spring-boot-modules/spring-boot-properties/src/test/java/com/baeldung/yaml/YAMLIntegrationTest.java
@@ -11,6 +11,7 @@ import org.springframework.test.context.junit4.SpringRunner;
@RunWith(SpringRunner.class)
@SpringBootTest(classes = MyApplication.class)
+@TestPropertySource(properties = {"spring.profiles.active = test"})
class YAMLIntegrationTest {
@Autowired
@@ -20,5 +21,6 @@ class YAMLIntegrationTest {
void whenProfileTest_thenNameTesting() {
assertTrue("testing".equalsIgnoreCase(config.getEnvironment()));
assertTrue("test-YAML".equalsIgnoreCase(config.getName()));
+ assertTrue("myurl".equalsIgnoreCase(config.getComponent().getIdm().getUrl()));
}
}
diff --git a/spring-cloud-data-flow/apache-spark-job/pom.xml b/spring-cloud-data-flow/apache-spark-job/pom.xml
index 65a57671ea..a4816a30ba 100644
--- a/spring-cloud-data-flow/apache-spark-job/pom.xml
+++ b/spring-cloud-data-flow/apache-spark-job/pom.xml
@@ -22,16 +22,6 @@
org.apache.spark
spark-core_${scala.version}
${spark.version}
-
-
- log4j
- log4j
-
-
- org.slf4j
- slf4j-log4j12
-
-
diff --git a/spring-cloud-data-flow/apache-spark-job/src/main/java/com/baeldung/spring/cloud/PiApproximation.java b/spring-cloud-data-flow/apache-spark-job/src/main/java/com/baeldung/spring/cloud/PiApproximation.java
index dfead21728..3f7c3be678 100644
--- a/spring-cloud-data-flow/apache-spark-job/src/main/java/com/baeldung/spring/cloud/PiApproximation.java
+++ b/spring-cloud-data-flow/apache-spark-job/src/main/java/com/baeldung/spring/cloud/PiApproximation.java
@@ -13,7 +13,7 @@ import java.util.stream.IntStream;
public class PiApproximation {
public static void main(String[] args) {
- SparkConf conf = new SparkConf().setAppName("BaeldungPIApproximation");
+ SparkConf conf = new SparkConf().setAppName("BaeldungPIApproximation").setMaster("local[2]");
JavaSparkContext context = new JavaSparkContext(conf);
int slices = args.length >= 1 ? Integer.valueOf(args[0]) : 2;
int n = (100000L * slices) > Integer.MAX_VALUE ? Integer.MAX_VALUE : 100000 * slices;
diff --git a/spring-cloud-data-flow/batch-job/pom.xml b/spring-cloud-data-flow/batch-job/pom.xml
index e11df0df8e..6b02b000e1 100644
--- a/spring-cloud-data-flow/batch-job/pom.xml
+++ b/spring-cloud-data-flow/batch-job/pom.xml
@@ -11,9 +11,8 @@
com.baeldung
- parent-boot-1
+ spring-cloud-data-flow
0.0.1-SNAPSHOT
- ../../parent-boot-1
@@ -21,7 +20,7 @@
org.springframework.cloud
spring-cloud-dependencies
- Brixton.SR5
+ Hoxton.SR4
pom
import
@@ -31,8 +30,7 @@
org.springframework.cloud
- spring-cloud-task-starter
- ${spring-cloud-task-starter.version}
+ spring-cloud-starter-task
@@ -48,7 +46,7 @@
- 1.0.3.RELEASE
+ 2.2.3.RELEASE
diff --git a/spring-cloud-data-flow/pom.xml b/spring-cloud-data-flow/pom.xml
index e2a0664f30..5b516146ae 100644
--- a/spring-cloud-data-flow/pom.xml
+++ b/spring-cloud-data-flow/pom.xml
@@ -9,8 +9,9 @@
com.baeldung
- parent-modules
- 1.0.0-SNAPSHOT
+ parent-boot-2
+ 0.0.1-SNAPSHOT
+ ../parent-boot-2
diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-mongodb-sink/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-mongodb-sink/pom.xml
index 9fd378b171..43772505a4 100644
--- a/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-mongodb-sink/pom.xml
+++ b/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-mongodb-sink/pom.xml
@@ -9,9 +9,8 @@
com.baeldung
- parent-boot-2
+ spring-cloud-data-flow-etl
0.0.1-SNAPSHOT
- ../../../parent-boot-2
@@ -63,7 +62,7 @@
UTF-8
UTF-8
- Greenwich.RELEASE
+ Hoxton.SR4
diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-transform/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-transform/pom.xml
index fdec22f3b3..f0d18a1c4f 100644
--- a/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-transform/pom.xml
+++ b/spring-cloud-data-flow/spring-cloud-data-flow-etl/customer-transform/pom.xml
@@ -9,9 +9,8 @@
com.baeldung
- parent-boot-2
+ spring-cloud-data-flow-etl
0.0.1-SNAPSHOT
- ../../../parent-boot-2
@@ -55,7 +54,7 @@
UTF-8
UTF-8
- Greenwich.RELEASE
+ Hoxton.SR4
diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml
index 02d572aeb7..186e5b1886 100644
--- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml
+++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/log-sink/pom.xml
@@ -10,9 +10,8 @@
com.baeldung
- parent-boot-1
+ spring-cloud-data-flow-stream-processing
0.0.1-SNAPSHOT
- ../../../parent-boot-1
@@ -35,7 +34,7 @@
- Brixton.SR7
+ Hoxton.SR4
diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml
index 5342049d73..e794287e10 100644
--- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml
+++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/pom.xml
@@ -2,6 +2,7 @@
4.0.0
+ com.baeldung
spring-cloud-data-flow-stream-processing
0.0.1-SNAPSHOT
spring-cloud-data-flow-stream-processing
@@ -9,9 +10,8 @@
com.baeldung
- parent-modules
- 1.0.0-SNAPSHOT
- ../../
+ spring-cloud-data-flow
+ 0.0.1-SNAPSHOT
@@ -21,5 +21,13 @@
time-processor
log-sink
+
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
+
diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml
index f8db434423..b4ad84cfe9 100644
--- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml
+++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-processor/pom.xml
@@ -10,9 +10,8 @@
com.baeldung
- parent-boot-1
+ spring-cloud-data-flow-stream-processing
0.0.1-SNAPSHOT
- ../../../parent-boot-1
@@ -35,7 +34,7 @@
- Brixton.SR7
+ Hoxton.SR4
diff --git a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml
index 8194755814..05908d3c52 100644
--- a/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml
+++ b/spring-cloud-data-flow/spring-cloud-data-flow-stream-processing/time-source/pom.xml
@@ -2,6 +2,7 @@
4.0.0
+ com.baeldung.spring.cloud
time-source
0.0.1-SNAPSHOT
time-source
@@ -10,9 +11,8 @@
com.baeldung
- parent-boot-1
+ spring-cloud-data-flow-stream-processing
0.0.1-SNAPSHOT
- ../../../parent-boot-1
@@ -35,7 +35,7 @@
- Brixton.SR7
+ Hoxton.SR4
diff --git a/spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml
index 1cfbf7e7c8..c9a73b9aa1 100644
--- a/spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml
+++ b/spring-cloud/spring-cloud-stream-starters/twitterhdfs/pom.xml
@@ -9,10 +9,10 @@
jar
- com.baeldung
- parent-boot-1
- 0.0.1-SNAPSHOT
- ../../../parent-boot-1
+ org.springframework.boot
+ spring-boot-starter-parent
+ 2.1.13.RELEASE
+
@@ -32,6 +32,11 @@
javax.servlet
jstl
+
+ org.springframework.boot
+ spring-boot-starter-test
+ test
+
@@ -45,7 +50,7 @@
- 1.3.1.RELEASE
+ 2.1.2.RELEASE
\ No newline at end of file
diff --git a/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GlobalEventBus.java b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GlobalEventBus.java
index 8b3c528c4d..fff9eb8a0f 100644
--- a/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GlobalEventBus.java
+++ b/spring-core-4/src/main/java/com/baeldung/beanpostprocessor/GlobalEventBus.java
@@ -8,7 +8,7 @@ import java.util.concurrent.Executors;
@SuppressWarnings("ALL")
public final class GlobalEventBus {
- public static final String GLOBAL_EVENT_BUS_EXPRESSION = "T(com.baeldung.postprocessor.GlobalEventBus).getEventBus()";
+ public static final String GLOBAL_EVENT_BUS_EXPRESSION = "T(com.baeldung.beanpostprocessor.GlobalEventBus).getEventBus()";
private static final String IDENTIFIER = "global-event-bus";
diff --git a/spring-resttemplate-2/README.md b/spring-resttemplate-2/README.md
new file mode 100644
index 0000000000..e0a394c642
--- /dev/null
+++ b/spring-resttemplate-2/README.md
@@ -0,0 +1,7 @@
+## Spring RestTemplate
+
+This module contains articles about Spring RestTemplate
+
+### Relevant Articles:
+
+
diff --git a/spring-resttemplate-2/pom.xml b/spring-resttemplate-2/pom.xml
new file mode 100644
index 0000000000..1a594fd21e
--- /dev/null
+++ b/spring-resttemplate-2/pom.xml
@@ -0,0 +1,63 @@
+
+
+ 4.0.0
+ spring-resttemplate
+ 0.1-SNAPSHOT
+ spring-resttemplate-2
+ war
+
+
+ com.baeldung
+ parent-boot-2
+ 0.0.1-SNAPSHOT
+ ../parent-boot-2
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-starter-web
+
+
+ org.springframework.boot
+ spring-boot-starter-test
+
+
+
+
+
+ org.springframework
+ spring-web
+
+
+ commons-logging
+ commons-logging
+
+
+
+
+
+
+ org.slf4j
+ slf4j-api
+
+
+ ch.qos.logback
+ logback-classic
+
+
+
+
+
+
+
+ org.springframework.boot
+ spring-boot-maven-plugin
+
+
+
+
+
diff --git a/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java
new file mode 100644
index 0000000000..4fa14edda1
--- /dev/null
+++ b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/RestTemplateConfigurationApplication.java
@@ -0,0 +1,14 @@
+package com.baeldung.resttemplate.logging;
+
+import org.springframework.boot.SpringApplication;
+import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
+import org.springframework.boot.autoconfigure.SpringBootApplication;
+
+@SpringBootApplication
+@EnableAutoConfiguration
+public class RestTemplateConfigurationApplication {
+
+ public static void main(String[] args) {
+ SpringApplication.run(RestTemplateConfigurationApplication.class, args);
+ }
+}
diff --git a/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/web/controller/PersonController.java b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/web/controller/PersonController.java
new file mode 100644
index 0000000000..8436c52a4a
--- /dev/null
+++ b/spring-resttemplate-2/src/main/java/com/baeldung/resttemplate/logging/web/controller/PersonController.java
@@ -0,0 +1,17 @@
+package com.baeldung.resttemplate.logging.web.controller;
+
+import java.util.Arrays;
+import java.util.List;
+
+import org.springframework.web.bind.annotation.PostMapping;
+import org.springframework.web.bind.annotation.RestController;
+
+@RestController
+public class PersonController {
+
+ @PostMapping("/persons")
+ public List getPersons() {
+ return Arrays.asList(new String[] { "Lucie", "Jackie", "Danesh", "Tao" });
+ }
+
+}
\ No newline at end of file
diff --git a/spring-resttemplate-2/src/main/resources/application.properties b/spring-resttemplate-2/src/main/resources/application.properties
new file mode 100644
index 0000000000..ea4f7c866d
--- /dev/null
+++ b/spring-resttemplate-2/src/main/resources/application.properties
@@ -0,0 +1,2 @@
+server.port=8080
+server.servlet.context-path=/spring-rest
\ No newline at end of file
diff --git a/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java
new file mode 100644
index 0000000000..17ce390d8a
--- /dev/null
+++ b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/LoggingInterceptor.java
@@ -0,0 +1,29 @@
+package com.baeldung.resttemplate.logging;
+
+import java.io.BufferedReader;
+import java.io.IOException;
+import java.io.InputStreamReader;
+import java.nio.charset.StandardCharsets;
+import java.util.stream.Collectors;
+import org.slf4j.Logger;
+import org.slf4j.LoggerFactory;
+import org.springframework.http.HttpRequest;
+import org.springframework.http.client.ClientHttpRequestExecution;
+import org.springframework.http.client.ClientHttpRequestInterceptor;
+import org.springframework.http.client.ClientHttpResponse;
+
+public class LoggingInterceptor implements ClientHttpRequestInterceptor {
+
+ final static Logger log = LoggerFactory.getLogger(LoggingInterceptor.class);
+
+ @Override
+ public ClientHttpResponse intercept(HttpRequest req, byte[] reqBody, ClientHttpRequestExecution ex) throws IOException {
+ log.debug("Request body: {}", new String(reqBody, StandardCharsets.UTF_8));
+ ClientHttpResponse response = ex.execute(req, reqBody);
+ InputStreamReader isr = new InputStreamReader(response.getBody(), StandardCharsets.UTF_8);
+ String body = new BufferedReader(isr).lines()
+ .collect(Collectors.joining("\n"));
+ log.debug("Response body: {}", body);
+ return response;
+ }
+}
diff --git a/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java
new file mode 100644
index 0000000000..99d0201eff
--- /dev/null
+++ b/spring-resttemplate-2/src/test/java/com/baeldung/resttemplate/logging/RestTemplateLoggingLiveTest.java
@@ -0,0 +1,50 @@
+package com.baeldung.resttemplate.logging;
+
+import static org.hamcrest.CoreMatchers.equalTo;
+
+import static org.hamcrest.MatcherAssert.assertThat;
+
+import java.util.ArrayList;
+import java.util.List;
+import org.junit.Test;
+import org.junit.runner.RunWith;
+import org.springframework.boot.test.context.SpringBootTest;
+import org.springframework.http.HttpStatus;
+import org.springframework.http.ResponseEntity;
+import org.springframework.http.client.ClientHttpRequestInterceptor;
+import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
+import org.springframework.test.context.junit4.SpringRunner;
+import org.springframework.util.CollectionUtils;
+import org.springframework.web.client.RestTemplate;
+
+@RunWith(SpringRunner.class)
+@SpringBootTest
+public class RestTemplateLoggingLiveTest {
+
+ private static final String baseUrl = "http://localhost:8080/spring-rest";
+
+ @Test
+ public void givenHttpClientConfiguration_whenSendGetForRequestEntity_thenRequestResponseFullLog() {
+
+ RestTemplate restTemplate = new RestTemplate();
+ restTemplate.setRequestFactory(new HttpComponentsClientHttpRequestFactory());
+
+ final ResponseEntity response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class);
+ assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
+ }
+
+ @Test
+ public void givenLoggingInterceptorConfiguration_whenSendGetForRequestEntity_thenRequestResponseCustomLog() {
+
+ RestTemplate restTemplate = new RestTemplate();
+ List interceptors = restTemplate.getInterceptors();
+ if (CollectionUtils.isEmpty(interceptors)) {
+ interceptors = new ArrayList<>();
+ }
+ interceptors.add(new LoggingInterceptor());
+ restTemplate.setInterceptors(interceptors);
+
+ final ResponseEntity response = restTemplate.postForEntity(baseUrl + "/persons", "my request body", String.class);
+ assertThat(response.getStatusCode(), equalTo(HttpStatus.OK));
+ }
+}
diff --git a/spring-resttemplate-2/src/test/resources/application.properties b/spring-resttemplate-2/src/test/resources/application.properties
new file mode 100644
index 0000000000..7bc9e56041
--- /dev/null
+++ b/spring-resttemplate-2/src/test/resources/application.properties
@@ -0,0 +1,5 @@
+logging.level.org.springframework.web.client.RestTemplate=DEBUG
+logging.level.com.baeldung.resttemplate.logging.LoggingInterceptor=DEBUG
+logging.level.org.apache.http=DEBUG
+logging.level.httpclient.wire=DEBUG
+logging.pattern.console=%20logger{20} - %msg%n