fiddler代理
RestTemplate restTemplate = new RestTemplate();
SimpleClientHttpRequestFactory reqfac = new SimpleClientHttpRequestFactory();
reqfac.setProxy(new Proxy(Proxy.Type.HTTP, new InetSocketAddress("127.0.0.1", 8888)));
restTemplate.setRequestFactory(reqfac);
三种类型
image.png
object
RequestObject request = new RequestObject();
request.setAge(18);
request.setName("小芳");
request.setAddress("广东深圳");
RestTemplate restTemplate = new RestTemplate();
HttpEntity request2 = new HttpEntity<>(request);
String url = "http://localhost:8080/user/object";
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request2, String.class);
System.out.println(result);
map
Map request = new HashMap<>();
request.put("age", 18);
request.put("name", "小芳");
request.put("address", "广东深圳");
RestTemplate restTemplate = new RestTemplate();
HttpEntity request2 = new HttpEntity<>(request);
String url = "http://localhost:8080/user/map";
ResponseEntity<String> result = restTemplate.exchange(url, HttpMethod.POST, request2, String.class);
form
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
MultiValueMap<String, String> map= new LinkedMultiValueMap<>();
map.add("id", "11");
HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(map, headers);
String fooResourceUrl
= "http://localhost:8080/user/form";
ResponseEntity<String> result = restTemplate.postForEntity(
fooResourceUrl , request , String.class);
System.out.println("result:" + result);
网友评论