方法一:template.exchange可以传请求参数
//get
HttpHeaders headers = new HttpHeaders();
RestTemplate template =new RestTemplate();
headers.add("Accept", "application/json");//请求头
headers.add("token", "435ee5bf5577b87a3c971edda0023f8b");//请求头
final String url = "http://127.0.0.1:8085/nuws/wireless/wlanalarm/list?page=1&limit=10";//url地址
HttpEntity<String> requestEntity = new HttpEntity<String>(null,
headers);
ResponseEntity<String> response = template.exchange(url,HttpMethod.GET,requestEntity,String.class);
String a = response.getBody();
System.out.println("re"+a);
方法二:template.postForObject可以传请求参数
HttpHeaders headers = new HttpHeaders();
RestTemplate template =new RestTemplate();
headers.add("Accept", "application/json");//请求头
headers.add("token", "435ee5bf5577b87a3c971edda0023f8b");//请求头
MultiValueMap<String, String> postParameters = new LinkedMultiValueMap<String, String>();
postParameters.add("deptId", "30001821");
postParameters.add("ipList", "192.1.1.1,192.3.2.1");
postParameters.add("alarmGrd", "1");
HttpEntity<MultiValueMap<String, String>> requestEntity = new HttpEntity<MultiValueMap<String, String>>(
postParameters, headers);
String obj = template.postForObject(
"http://127.0.0.1:8085/nuws/wireless/getAlarmphone/phone", requestEntity,
String.class);
System.out.println("obj"+obj);
传递参数为对象:
HttpHeaders headers = new HttpHeaders(); RestTemplate template =new RestTemplate(); headers.add("Accept", "application/json"); SysLoginForm sysLoginForm = new SysLoginForm(); sysLoginForm.setUsername("admin"); sysLoginForm.setPassword("admin"); HttpEntity requestEntity = new HttpEntity( sysLoginForm, headers); String obj = template.postForObject( "http://127.0.0.1:8085/nuws/sys/login", requestEntity, String.class); System.out.println("obj"+obj);