diff --git a/spring-core-4/pom.xml b/spring-core-4/pom.xml
index 6ba8357a0e..492a2ec5a2 100644
--- a/spring-core-4/pom.xml
+++ b/spring-core-4/pom.xml
@@ -71,6 +71,11 @@
javax.annotation-api
${annotation-api.version}
+
+ org.apache.commons
+ commons-text
+ ${apache-commons-text.version}
+
@@ -80,6 +85,7 @@
4.0.0
1.3.2
3.3.2
+ 1.10.0
\ No newline at end of file
diff --git a/spring-core-4/src/main/java/com/baeldung/escapehtml/HtmlEscapeUtils.java b/spring-core-4/src/main/java/com/baeldung/escapehtml/HtmlEscapeUtils.java
new file mode 100644
index 0000000000..b7da2eefb8
--- /dev/null
+++ b/spring-core-4/src/main/java/com/baeldung/escapehtml/HtmlEscapeUtils.java
@@ -0,0 +1,21 @@
+package com.baeldung.escapehtml;
+
+import com.google.common.html.HtmlEscapers;
+import org.apache.commons.text.StringEscapeUtils;
+import org.springframework.web.util.HtmlUtils;
+
+public class HtmlEscapeUtils {
+
+ public static String escapeWithApacheCommons(String input) {
+ return StringEscapeUtils.escapeHtml4(input);
+ }
+
+ public static String escapeWithGuava(String input) {
+ return HtmlEscapers.htmlEscaper().escape(input);
+ }
+
+ public static String escapeWithSpring(String input) {
+ return HtmlUtils.htmlEscape(input);
+ }
+
+}
diff --git a/spring-core-4/src/test/java/com/baeldung/escapehtml/HtmlEscapeUnitTest.java b/spring-core-4/src/test/java/com/baeldung/escapehtml/HtmlEscapeUnitTest.java
new file mode 100644
index 0000000000..92d1138869
--- /dev/null
+++ b/spring-core-4/src/test/java/com/baeldung/escapehtml/HtmlEscapeUnitTest.java
@@ -0,0 +1,43 @@
+package com.baeldung.escapehtml;
+
+import org.junit.Test;
+
+import static org.junit.Assert.assertEquals;
+
+public class HtmlEscapeUnitTest {
+
+ @Test
+ public void whenInputContainAmpersand_thenEscape() {
+ String input = "AT&T";
+ String expected = "AT&T";
+ assertEquals(expected, HtmlEscapeUtils.escapeWithApacheCommons(input));
+ assertEquals(expected, HtmlEscapeUtils.escapeWithGuava(input));
+ assertEquals(expected, HtmlEscapeUtils.escapeWithSpring(input));
+ }
+
+ @Test
+ public void whenInputContainDoubleQuotes_thenEscape() {
+ String input = "She said, \"Hello!\"";
+ String expected = "She said, "Hello!"";
+ assertEquals(expected, HtmlEscapeUtils.escapeWithApacheCommons(input));
+ assertEquals(expected, HtmlEscapeUtils.escapeWithGuava(input));
+ assertEquals(expected, HtmlEscapeUtils.escapeWithSpring(input));
+ }
+
+ @Test
+ public void whenInputContainManyHtmlSymbols_thenEscape() {
+ String input = "
This is a test string.
";
+ String expected = "<p>This is a <strong>test</strong> string.</p>";
+ assertEquals(expected, HtmlEscapeUtils.escapeWithApacheCommons(input));
+ assertEquals(expected, HtmlEscapeUtils.escapeWithGuava(input));
+ assertEquals(expected, HtmlEscapeUtils.escapeWithSpring(input));
+ }
+
+ @Test
+ public void whenInputContainNoHtmlSymbols_thenEscape() {
+ String input = "This is a plain text.";
+ assertEquals(input, HtmlEscapeUtils.escapeWithApacheCommons(input));
+ assertEquals(input, HtmlEscapeUtils.escapeWithGuava(input));
+ assertEquals(input, HtmlEscapeUtils.escapeWithSpring(input));
+ }
+}