getForObject
String url = "http://127.0.0.1:8080/user/get?id={1}";
String id = "11111";
ResultDto result = restTemplate.getForObject(url, ResultDto.class, id);
postForObject
JSON参数
String url = "xxxx";
UserInfo userInfo = new UserInfo();
userInfo.setName("ezio");
ResultDto result = restTemplate.postForObject(url, userInfo, ResultDto.class);
表单参数
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
String url = "xxxx";
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("param1", "1");
params.add("param2", "2");
ResultDto result = restTemplate.postForObject(url, params, ResultDto.class);
带请求头
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
String url = "xxxx";
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("param1", "1");
params.add("param2", "2");
HttpHeaders headers = new HttpHeaders();
headers.add("Content-Type", "application/x-www-form-urlencoded");
HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(params, headers);
ResultDto result = restTemplate.postForObject(url, httpEntity, ResultDto.class);