---
title: "restTemplate.postForObject两个方法"
author: "Andy_Dou"
date: "2014-08-13T15:11:38Z"
tags: []
categories: ["开源软件"]
slug: "resttemplate-postforobject"
draft: false
lastmod: "2014-08-13T15:11:38Z"
id: 2103506
url: "https://ideabean.iteye.com/blog/2103506"
---

1、restTemplate.postForObject(url, null, String.class, params);

Example:

UriTemplate template = new UriTemplate(“http://example.com/hotels/{hotel}/bookings/{booking}”);  
Map&lt;String, String&gt; uriVariables = new HashMap&lt;String, String&gt;();  
uriVariables.put(“booking”, “42″);  
uriVariables.put(“hotel”, “1″);  
System.out.println(template.expand(uriVariables));

will print:   
http://example.com/hotels/1/bookings/42

 

2、postForObject(URI url, Object request, Class&lt;T&gt; responseType)

url中不用加参数  
Create a new resource by POSTing the given object to the URL, and returns the representation found in the response.

The request parameter can be a HttpEntity in order to add additional HTTP headers to the request.