gpt4 book ai didi

java - Java 中的 RestTemplate - RestTemplate 没有足够的变量值

转载 作者:行者123 更新时间:2023-12-01 22:03:40 33 4
gpt4 key购买 nike

我有以下内容:

final String notification = "{\"DATA\"}";
final String url = "http://{DATA}/create";
ResponseEntity<String> auth = create(address, name, psswd, url);

攻击此方法:

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);
}

我认为它应该有效,但它让我感到困惑

org.springframework.web.util.NestedServletException: Request processing failed; nested exception is java.lang.IllegalArgumentException: Not enough variable values available to expand 'notification'

为什么?

最佳答案

正如您问题的重复内容所示,您的网址中有 { }

Spring 将尝试填充该{notification},但由于您没有提供它,它会失败并提示没有足够的变量值可用于扩展“通知”

您只需要传递通知字符串,Spring 就可以正确构建 URL。

这是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

因此,您需要将通知字符串作为第四个参数传递,如下所示:

restTemplate.postForEntity(url, entity, String.class, notification);

关于java - Java 中的 RestTemplate - RestTemplate 没有足够的变量值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33214059/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com