RestTemplate调用

RestTemplate调用

一、get请求
public class TestController{ @Autowired RestTemplate restTemplate; public void getlist(String a,String b) { ResponseEntity<ResultVO<List<TClNew>>> result = restTemplate.exchange("http://192.168.1.1/server/getlist/{a}/{b}", HttpMethod.GET, null, new ParameterizedTypeReference<ResultVO<List<test>>>() { }, a, b); }
}

二、post请求
public class TestController{
    @Autowired
    RestTemplate restTemplate;

  public void post(){
   HttpHeaders headers = new HttpHeaders();
   Map<String, Object> map = new HashMap<>();
   map.put("aaa", "qqqq");

    headers.setContentType(MediaType.MULTIPART_FORM_DATA);
    HttpEntity<MultiValueMap<String, Object>> httpEntity = new HttpEntity<>(map, headers);
    ResponseEntity<ResultVO<String>> response = restTemplate.exchange(url, HttpMethod.POST, httpEntity,
    new ParameterizedTypeReference<ResultVO<String>>() {
    });

  }

}