package com.liyuan.www;
import org.junit.Test;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
public class RestTemplateTest {
private RestTemplate restTemplate = new RestTemplate();
@Test
public void getTest() {
LinkedMultiValueMap<String, String> multiValueMap = new LinkedMultiValueMap<>();
//请求头
multiValueMap.add("name1", "liyuan");
multiValueMap.add("number2", "20181003114");
//请求体
Map<String, Object> body = new HashMap<>();
body.put("a", 'A');
HttpEntity entity = new HttpEntity<>(body, multiValueMap);
ResponseEntity<String> res = restTemplate.exchange("http://localhost:8080/test?name=liyuan&number=123456", HttpMethod.POST, entity, String.class);
System.out.println(res.getBody());
}
}
RestTemplate简易融合
最新推荐文章于 2024-10-16 10:13:45 发布