繁体   English   中英

春天resttemplate

[英]Spring resttemplate

如何发送POST请求,消息正文中必须是查询参数?

我试过了:

MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("name", "xx");
map.add("password", "xx");

restTemplate.postForObject("URL", map, Response.class);

但这是行不通的。 我想将数据发送到Bitstamp API。

编辑:我的春豆似乎:

<bean id="restTemplate" class="org.springframework.web.client.RestTemplate">
    <property name="messageConverters">
        <list>
            <bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter" />
        </list>
    </property>
</bean>

编辑2:我的代码似乎

List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);// or any other

HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);

HttpEntity<String> requestEntity = new HttpEntity<String>("key=XX&nonce=XX&signature=XX", headers);
ResponseEntity<AccountBalance> responseEntity = restTemplate.exchange(
"https://www.bitstamp.net/api/balance/", HttpMethod.POST, requestEntity, AccountBalance.class);

现在的响应是:缺少密钥,签名和随机数参数

但是应该是:找不到API密钥

你必须使用restTemplate.exchange

例:

List<MediaType> acceptableMediaTypes = new ArrayList<MediaType>();
acceptableMediaTypes.add(MediaType.APPLICATION_JSON);// or any other

HttpHeaders headers = new HttpHeaders();
headers.setAccept(acceptableMediaTypes);

HttpEntity<String> requestEntity = new HttpEntity<String>("name=XX&password=XX",headers);
ResponseEntity<Response> responseEntity = restTemplate.exchange("URL", HttpMethod.POST, requestEntity, Response.class);

现在可以了,谢谢大家:-)

MultiValueMap<String, String> body = new LinkedMultiValueMap<String, String>();
body.add("key", "XXX");
body.add("nonce", "XX");
body.add("signature", "XX");

HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);

HttpEntity<?> requestEntity = new HttpEntity<Object>(body, headers);

ResponseEntity<AccountBalance> responseEntity = restTemplate.exchange(bitstampBalanceUrl, HttpMethod.POST,
                requestEntity, AccountBalance.class);

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM