你out了,赶紧换
RestTemplate 吧!
进入正题,直接实战!!!
import java.util.HashMap;
import java.util.Map;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.context.SpringBootTest;
import org.springframework.context.annotation.Bean;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.test.context.junit4.SpringRunner;
import org.springframework.util.LinkedMultiValueMap;
import org.springframework.util.MultiValueMap;
import org.springframework.web.client.RestTemplate;
@RunWith(SpringRunner.class)
@SpringBootTest(webEnvironment = SpringBootTest.WebEnvironment.RANDOM_PORT)
public class TestUrl {
@Autowired
private RestTemplate restTemplate;
/*
* get无参
*/
@Test
public void testNoParameter() {
String object = restTemplate.getForObject(
"http://127.0.0.1/findAllStorageDevice", String.class);
System.out.println("11111111111" + object);
}
/*
* get有参
*/
@Test
public void testYesParameter() {
Map<String, String> map = new HashMap();
map.put("sdId", "res$cc$20180524113123$962c4ded-d1df-49ca-92d3-cfbca5eb28ea");
String object = restTemplate.getForObject(
"http://127.0.0.1/findStorageDeviceById?sdId={sdId}",
String.class, map);
System.out.println("11111111111" + object);
}
/*
* post
*/
@Test
public void testPost() {
MultiValueMap<String, String> map = new LinkedMultiValueMap<String, String>();
map.add("username", "11");
map.add("password", "22");
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<MultiValueMap<String, String>>(map, headers);
ResponseEntity<String> postForEntity = restTemplate.postForEntity("http://127.0.0.1/login", map,
String.class);
System.out.println("11111111111" + postForEntity);
}
}