如何发送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。
编辑:我的 Spring Bean 似乎:
<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);
现在的响应:缺少 key 、签名和随机数参数
但应该是:未找到API key
最佳答案
你必须使用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);
关于java - Spring 休息模板,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21559325/