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.
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.
Then call RestTemplate with the URI:
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.
For broader consistency, you can also configure the URI template handler on the RestTemplate so values are encoded in a safer way across requests.
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:
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
RestTemplateURI 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
UriComponentsBuilderor URI template variables instead of string concatenation. - Calling
RestTemplatewith aURIis usually safer than handing it a prebuilt URL string. - Treat URI construction as structured data, not string assembly.
Related reading
- a bean of type 'org.springframework.mail.javamail.JavaMailSender' that could not be found
- A default binder has been requested, but there are no binders available for ''org.springframework.cloud.stream.messaging.DirectWithAttributesChannel''
- A failure occurred while executing com.android.build.gradle.internal.tasks
- A failure occurred while executing org.jetbrains.kotlin.gradle.internal.KaptExecution
- A for-loop to iterate over an enum in Java
- A generic list of anonymous class
- A Java collection of value pairs? (tuples?)
- A KeyValuePair in Java

OOD Fundamentals
Master object-oriented design from first principles, SOLID, design patterns, and classic interview problems with hands-on coding.
View the courseData 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.