调用Spring RestController时,使用Spring的RestTemplate转义URL变量的正确方法是什么? [英] What's the proper way to escape URL variables with Spring's RestTemplate when calling a Spring RestController?
问题描述
调用RestTemplate.exchange
进行获取请求时,例如:
When calling RestTemplate.exchange
to do a get request, such as:
String foo = "fo+o";
String bar = "ba r";
restTemplate.exchange("http://example.com/?foo={foo}&bar={bar}", HttpMethod.GET, null, foo, bar)
正确地对get请求转义URL变量是什么?
what's the proper to have the URL variables correctly escaped for the get request?
Specifically, how do I get pluses (+
) correctly escaped because Spring is interpreting as spaces, so, I need to encode them.
我尝试像这样使用UriComponentsBuilder
:
String foo = "fo+o";
String bar = "ba r";
UriComponentsBuilder ucb = UriComponentsBuilder.fromUriString("http://example.com/?foo={foo}&bar={bar}");
System.out.println(ucb.build().expand(foo, bar).toUri());
System.out.println(ucb.build().expand(foo, bar).toString());
System.out.println(ucb.build().expand(foo, bar).toUriString());
System.out.println(ucb.build().expand(foo, bar).encode().toUri());
System.out.println(ucb.build().expand(foo, bar).encode().toString());
System.out.println(ucb.build().expand(foo, bar).encode().toUriString());
System.out.println(ucb.buildAndExpand(foo, bar).toUri());
System.out.println(ucb.buildAndExpand(foo, bar).toString());
System.out.println(ucb.buildAndExpand(foo, bar).toUriString());
System.out.println(ucb.buildAndExpand(foo, bar).encode().toUri());
System.out.println(ucb.buildAndExpand(foo, bar).encode().toString());
System.out.println(ucb.buildAndExpand(foo, bar).encode().toUriString());
并显示:
http://example.com/?foo=fo+o&bar=ba%20r
http://example.com/?foo=fo+o&bar=ba r
http://example.com/?foo=fo+o&bar=ba r
http://example.com/?foo=fo+o&bar=ba%20r
http://example.com/?foo=fo+o&bar=ba%20r
http://example.com/?foo=fo+o&bar=ba%20r
http://example.com/?foo=fo+o&bar=ba%20r
http://example.com/?foo=fo+o&bar=ba r
http://example.com/?foo=fo+o&bar=ba r
http://example.com/?foo=fo+o&bar=ba%20r
http://example.com/?foo=fo+o&bar=ba%20r
http://example.com/?foo=fo+o&bar=ba%20r
在某些情况下,空格可以正确地转义,但是加号永远不会转义.
The space is correctly escaped in some instances, but the plus is never escaped.
我也这样尝试过UriTemplate
:
String foo = "fo+o";
String bar = "ba r";
UriTemplate uriTemplate = new UriTemplate("http://example.com/?foo={foo}&bar={bar}");
Map<String, String> vars = new HashMap<>();
vars.put("foo", foo);
vars.put("bar", bar);
URI uri = uriTemplate.expand(vars);
System.out.println(uri);
具有完全相同的结果:
http://example.com/?foo=fo+o&bar=ba%20r
推荐答案
显然,正确的方法是定义工厂并更改编码模式:
Apparently, the correct way of doing this is by defining a factory and changing the encoding mode:
String foo = "fo+o";
String bar = "ba r";
DefaultUriBuilderFactory factory = new DefaultUriBuilderFactory();
factory.setEncodingMode(DefaultUriBuilderFactory.EncodingMode.VALUES_ONLY);
URI uri = factory.uriString("http://example.com/?foo={foo}&bar={bar}").build(foo, bar);
System.out.println(uri);
打印出来:
http://example.com/?foo=fo%2Bo&bar=ba%20r
此处记录了以下内容: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#web-uri-encoding
This is documented here: https://docs.spring.io/spring/docs/current/spring-framework-reference/web.html#web-uri-encoding
这篇关于调用Spring RestController时,使用Spring的RestTemplate转义URL变量的正确方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!