Suppose you want to encode query parameters with code like the following.
MultiValueMap<String, String> params = new LinkedMultiValueMap<>();
params.add("key", "a+b");
String u = UriComponentsBuilder.fromHttpUrl("http://www.google.com").queryParams(params).build().encode().toString();
System.out.println(u);
The result of the above code depends on the version of spring-web as shown in the table below.
spring-web version | result |
---|---|
spring-boot1(spring-web-4.3.18.RELEASE) | http://www.google.com?key=a%2Bb |
spring-boot2(spring-web-5.0.4.RELEASE) | http://www.google.com?key=a+b |
In the new 5.0, the "+" is no longer encoded.
Implementation of query parameter encoding, as described around https://stackoverflow.com/questions/50432395/whats-the-proper-way-to-escape-url-variables-with-springs-resttemplate-when-ca Has changed to RFC 3986 base, so somehow.
Check the implementation. A class called HierarchicalUriComponents is actually in charge of encoding, and there is a part inside it that determines whether or not it is an encoding target.
This is the case in 4.3.
QUERY_PARAM {
@Override
public boolean isAllowed(int c) {
if ('=' == c || '+' == c || '&' == c) {
return false;
}
else {
return isPchar(c) || '/' == c || '?' == c;
}
}
},
In 5.0, this is the case, and you can see that the "+" is off.
QUERY_PARAM {
@Override
public boolean isAllowed(int c) {
if ('=' == c || '&' == c) {
return false;
}
else {
return isPchar(c) || '/' == c || '?' == c;
}
}
},
Recommended Posts