相关文章推荐
Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("name", "xx");
map.add("password", "xx");
restTemplate.postForObject("URL", map, Response.class);

But it doesn't work. I want to send data into bitstamp api.

EDIT: My spring bean seems:

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

EDIT 2: My code seems

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

Response is now: Missing key, signature and nonce parameters

But it should be: API key not found

query parameters in message body this is self contradictory. Query parameters are in the url, the request body is the request body. – Taylor Feb 4, 2014 at 17:30 Yes, but i did in SOAPUI and there is button Post Query string into body and then it's works. I can't do have the same result with restTemplate :-( – user1089362 Feb 4, 2014 at 17:42 You seem to be missing some core REST concepts. Maybe start here: spring.io/blog/2009/03/27/rest-in-spring-3-resttemplate but you may need a more fundamental understanding of REST first. – Taylor Feb 4, 2014 at 17:57
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);
                I don't uderstand why it doesn't work. Because when I use this method (POST request):  mkyong.com/java/how-to-send-http-request-getpost-in-java Then it works perfectly. Is it possible to do it with resttemplate?
– user1089362
                Feb 6, 2014 at 19:53
                share your code please , it could be the content type ... or something else, share what you are trying to do
– storm_buster
                Feb 6, 2014 at 21:01
                you are still using restTemplate.postForObject, while I advise you yo uer restTemplate.exchange
– storm_buster
                Feb 7, 2014 at 15:05

It works now, thank you all :-)

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

Thanks for contributing an answer to Stack Overflow!

  • Please be sure to answer the question. Provide details and share your research!

But avoid

  • Asking for help, clarification, or responding to other answers.
  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.

 
推荐文章