Java
RestTemplate
URL Encoding
Plus Sign Issue
Spring Framework

Plus sign not encoded with RestTemplate using String URL, interpreted as space

Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms

Introduction

A literal plus sign in a query parameter is not the same thing as a space, but many HTTP stacks treat + as a space when query strings are decoded in form-style contexts. That is why building a URL by hand as a raw string and then passing it to RestTemplate often produces surprising behavior: the server receives a space unless the plus sign was encoded as %2B.

Why This Happens

The key issue is query parameter encoding. In many web contexts:

  • a literal space becomes %20
  • a literal plus sign should become %2B
  • an unencoded + inside a query string is commonly interpreted as a space

So if your intended value is [email protected], sending it as plain text in the query string is risky. By the time the server parses the request, that + may already have been normalized to a space.

This is not really a RestTemplate bug as much as an encoding mismatch. The client code is handing over a URL that already contains ambiguous query data.

Avoid Manually Concatenating Query Strings

The safest pattern is to let Spring build the URI from parameter values, not to concatenate the URL yourself.

java
1import java.net.URI;
2import org.springframework.web.util.UriComponentsBuilder;
3
4String email = "[email protected]";
5
6URI uri = UriComponentsBuilder
7        .fromUriString("https://api.example.com/search")
8        .queryParam("email", email)
9        .build()
10        .encode()
11        .toUri();

Then call RestTemplate with the URI:

java
ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);

That makes Spring encode the query parameter value correctly, so the server sees %2B where needed.

Prefer URI Templates Over Raw String URLs

Another good option is to use URI template variables instead of embedding the raw value in the URL string.

java
1String template = "https://api.example.com/search?email={email}";
2String email = "[email protected]";
3
4ResponseEntity<String> response = restTemplate.getForEntity(
5        template,
6        String.class,
7        email
8);

For broader consistency, you can also configure the URI template handler on the RestTemplate so values are encoded in a safer way across requests.

java
1import org.springframework.web.client.RestTemplate;
2import org.springframework.web.util.DefaultUriBuilderFactory;
3
4DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
5factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
6
7RestTemplate restTemplate = new RestTemplate();
8restTemplate.setUriTemplateHandler(factory);

That helps when you are dealing with many requests and do not want each caller to reinvent URI construction.

What Not to Do

This pattern is fragile:

java
String url = "https://api.example.com/[email protected]";
restTemplate.getForObject(url, String.class);

The problem is that the plus sign is already sitting in the raw query string. By the time the request is processed, the server may read it as a space. Once the ambiguous string exists, you have already lost the clean separation between a URI template and a parameter value.

This is why encoding should happen at the value level, not after the full URL has been manually assembled. Good client code keeps query keys, values, and transport encoding as separate concerns until the last possible step.

Common Pitfalls

  • Concatenating query strings by hand instead of using a URI builder.
  • Assuming + is always transmitted literally in a query parameter.
  • Passing raw string URLs when the value contains reserved or ambiguous characters.
  • Encoding the full URL incorrectly instead of encoding the query parameter values.
  • Fixing one call site but leaving the RestTemplate URI handling inconsistent elsewhere.

Summary

  • A literal plus sign in query data should be encoded as %2B.
  • Raw string URLs are the main reason this problem appears with RestTemplate.
  • Use UriComponentsBuilder or URI template variables instead of string concatenation.
  • Calling RestTemplate with a URI is usually safer than handing it a prebuilt URL string.
  • Treat URI construction as structured data, not string assembly.

Course
Intermediate
27 lessons
14 hours
OOD Fundamentals

Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.

View the course
Data Structures & Algorithms practice on Codemia

Step through 300+ coding interview problems with animated visualisers that show the data structure changing as the code runs. The Blind 75 are free.

Practice algorithms