Escape URL in Java using Google Guava

Tags: Google Guava Escaper UrlEscapers urlFormParameterEscaper urlFragmentEscaper urlPathSegmentEscaper URL Escape URL

In this Java tutorial we learn how to escape URL form parameter names and values, fragment, path segments using the UrlEscapers class of Google Guava library.

How to add Google Guava library to the Java project

To use the Google Guava library in the Gradle build project, add the following dependency into the build.gradle file.

implementation group: 'com.google.guava', name: 'guava', version: '30.1.1-jre'

To use the Google Guava library in the Maven build project, add the following dependency into the pom.xml file.

<dependency>
    <groupId>com.google.guava</groupId>
    <artifactId>guava</artifactId>
    <version>30.1.1-jre</version>
</dependency>

To have more information about the Google Guava library you can visit the project home page at guava.dev

How to escape URL form parameter names and values in Java

In the following Java program example, we learn how to use the UrlEscapers.urlFormParameterEscaper() method of Google Guava library to escape strings so they can be safely included in URL form parameter names and values.

UrlFormParameterEscaperExample.java

import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;

public class UrlFormParameterEscaperExample {
    public static void main(String... args) {
        String parameterValue = "key2=value1&key2 = value2#test";

        Escaper escaper = UrlEscapers.urlFormParameterEscaper();
        String result = escaper.escape(parameterValue);

        System.out.println("URL Parameter String:\n" + parameterValue);
        System.out.println("\nEscaped String:\n" + result);
    }
}
The output is:
URL Parameter String:
key2=value1&key2 = value2#test

Escaped String:
key2%3Dvalue1%26key2+%3D+value2%23test

Or you can shortly escape a string with the below line of code.

String result = UrlEscapers.urlFormParameterEscaper().escape(parameterValue);

How to escape URL fragment in Java

In the following Java program example, we learn how to use the UrlEscapers.urlFragmentEscaper() method of Google Guava library to escape strings so they can be safely included in a URL fragment.

UrlFragmentEscaperExample.java

import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;

public class UrlFragmentEscaperExample {
    public static void main(String... args) {
        String fragmentValue = "https://simplesolution.dev/search/?q=html&key1=value1&key2 = value2#test";

        Escaper escaper = UrlEscapers.urlFragmentEscaper();
        String result = escaper.escape(fragmentValue);

        System.out.println("URL Fragment String:\n" + fragmentValue);
        System.out.println("\nEscaped String:\n" + result);
    }
}
The output is:
URL Fragment String:
https://simplesolution.dev/search/?q=html&key1=value1&key2 = value2#test

Escaped String:
https://simplesolution.dev/search/?q=html&key1=value1&key2%20=%20value2%23test

Or you can shortly escape a string with the below line of code.

String result = UrlEscapers.urlFragmentEscaper().escape(fragmentValue);

How to escape URL path segments in Java

In the following Java program example, we learn how to use the UrlEscapers.urlPathSegmentEscaper() method of Google Guava library to escape strings so they can be safely included in URL path segments.

UrlPathSegmentEscaperExample.java

import com.google.common.escape.Escaper;
import com.google.common.net.UrlEscapers;

public class UrlPathSegmentEscaperExample {
    public static void main(String... args) {
        String pathSegmentValue = "key2=value1&key2 = value2#test";

        Escaper escaper = UrlEscapers.urlPathSegmentEscaper();
        String result = escaper.escape(pathSegmentValue);

        System.out.println("URL Path Segment String:\n" + pathSegmentValue);
        System.out.println("\nEscaped String:\n" + result);
    }
}
The output is:
URL Path Segment String:
key2=value1&key2 = value2#test

Escaped String:
key2=value1&key2%20=%20value2%23test

Or you can shortly escape a string with the below line of code.

String result = UrlEscapers.urlPathSegmentEscaper().escape(pathSegmentValue);

Happy Coding 😊