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

时间:2015-10-19 12:11:25

标签: java spring spring-mvc url resttemplate

我有以下内容:

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'

为什么?

2 个答案:

答案 0 :(得分:3)

正如您问题的副本所示,您的网址中有{ }

Spring会尝试填充{notification},但由于您没有提供,因此无法说Not enough variable values available to expand '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

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

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

答案 1 :(得分:-1)

以下一行足以让它发挥作用。我摆脱了关键人物并且现在正在工作。

success: function (data) {
    var key = data['key'];
    addFilesToTable(key);
    return true;
}