Java 中的 RestTemplate - RestTemplate 没有足够的变量值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/33214059/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
RestTemplate in Java - Not enough variable values available with RestTemplate
提问by stack man
I've got the following:
我有以下几点:
final String notification = "{\"DATA\"}";
final String url = "http://{DATA}/create";
ResponseEntity<String> auth = create(address, name, psswd, url);
Attacking this method:
攻击这个方法:
private ResponseEntity<String> create(final String address, final String name,
final String newPassword, final String url) {
final Map<String, Object> param = new HashMap<>();
param.put("name", name);
param.put("password", newPassword);
param.put("email", address);
final HttpHeaders header = new HttpHeaders();
final HttpEntity<?> entity = new HttpEntity<Object>(param, header);
RestTemplate restTemplate = new RestTemplate();
return restTemplate.postForEntity(url, entity, String.class);
}
I think it should work, but it's throwing me a
我认为它应该有效,但它让我感到
org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Not enough variable values available to expand 'notification'
Why?
为什么?
回答by ESala
As the duplicates of your question say, you have { }
in your URL.
正如您的问题的重复项所说,您{ }
的网址中有。
Spring will try to fill that {notification}
but since you don't provide it, it fails saying Not enough variable values available to expand 'notification'
.
Spring 将尝试填充它,{notification}
但由于您不提供它,因此它无法说Not enough variable values available to expand 'notification'
.
You just need to pass the notification string so that Spring can build the URL correctly.
您只需要传递通知字符串,以便 Spring 可以正确构建 URL。
Here is the javadocfor this method:
这是此方法的javadoc:
public <T> ResponseEntity<T> postForEntity(String url,
Object request,
Class<T> responseType,
Object... uriVariables)
throws RestClientException
Parameters:
url - the URL
request - the Object to be POSTed, may be null
responseType - the class of the response
uriVariables - the variables to expand the template
So, you need to pass the notification string as a 4th parameter, like this:
因此,您需要将通知字符串作为第四个参数传递,如下所示:
restTemplate.postForEntity(url, entity, String.class, notification);
回答by stack man
The following line was just enough to get it working. I got rid of the key characters and it's working now.
以下行足以让它工作。我摆脱了关键角色,现在可以使用了。
final String url = "http://DATA/create";