问题描述
我正在使用 Spring Framework 中的 RestTemplate 用 Java 创建一个 REST 客户端.
I'm creating a REST Client in Java with RestTemplate from Spring Framework.
一切都很好,直到我必须用 postForLocation 发帖.
Everything is fine until i have to do a post with postForLocation.
我有权访问的网络服务返回一个 json,其中包含有关 POST ACTION 的信息.
The webservice i'm having access return a json with informations about the POST ACTION.
在 PHP 中很好,但我真的不明白如何在 Java 中使用 RestTemplate.
In PHP it's fine but i really don't understand how to do in Java with RestTemplate.
public String doLogin()
{
Map<String, String> args = new HashMap<String, String>();
args.put("email", AUTH_USER);
args.put("token", AUTH_PASS);
String result = restTemplate.postForLocation(API_URL + "account/authenticate/?email={email}&token={token}", String.class, args);
return result;
}
这将返回 NULL.
使用相同的代码但使用 getForObject(当然,将 URL 更改为正确的内容)我有一个完整的响应,即这有效:
With same code but using getForObject (and of course, changing the URL to something right) I have a full response, i.e. this works:
String result = restTemplate.getForObject(url, String.class);
那么...如何从 postForLocation 获得 RESPONSE?
So... how get the RESPONSE from a postForLocation?
Obs.:对不起,如果这个问题很愚蠢.我是 Java 初学者
Obs.: Sorry if this question is dumb. I'm beginner in Java
推荐答案
感谢其中一个答案,我找到了如何使用 postForObject 从带有 Spring 的 POST 获取响应
Thanks to one of answers i've figured out how get the response from a POST with Spring by using the postForObject
String result = restTemplate.postForObject(API_URL + "account/authenticate/?email="+ AUTH_USER +"&token="+ AUTH_PASS, null, String.class);
出于某种原因,我不能在 MAP 中使用参数,而必须将它们内嵌在 URL 中.但这对我来说很好.
For some reason i can't use arguments with MAP and have to put them inline in URL. But that's fine for me.
这篇关于如何从 RestTemplate postForLocation 获得 STRING 响应?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!