如果只是针对纯Rest接口处理的话,我们可以使用restTemplate对象来操作,简单方便,可以不需要手写httpClient代码了。
我们看下基本的用法,如下:
client:
@RestController
@RequestMapping("restTemp")
public class RestTemplateController {
@GetMapping("getTest1")
public void getTest1(){
String url = "http://具体iP地址/demo/demoTest?name={name}&email={email}";
Map<String,String> params = new HashMap<>();
params.put("name","yyc");
params.put("email","12306");
RestTemplate restTemplate = new RestTemplate();
String request = restTemplate.getForObject(url,String.class,params);
//String request = restTemplate.getForObject(url,String.class,"yyc","email");
System.out.println(request);
}
}
server:
@GetMapping(value = "demoTest", produces = MediaType.APPLICATION_JSON_VALUE)
public String demoTest(@RequestParam String name,@RequestParam String email){
System.out.println(name);
System.out.println(email);
return "success";
}
getForEntity
client:
@GetMapping("getTest2")
public void getTest2(){
String url = "http://具体iP地址:8080/demo/demoTest?name={name}&email={email}";
HttpHeaders headers = new HttpHeaders();
headers.set("phone","123456");
Map<String,Object> params = new HashMap<>();
params.put("name","yyc");
params.put("email","12306");
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(headers);
ResponseEntity<String> request = restTemplate.getForEntity(url,String.class,httpEntity,params);
System.out.println(request.getBody());
}
server:
@GetMapping(value = "demoTest1", produces = MediaType.APPLICATION_JSON_VALUE)
public String demoTest1(@RequestParam String name, @RequestParam String email, @RequestHeader String phone){
System.out.println(name);
System.out.println(email);
System.out.println(phone);
return "success";
}
postForObject
这里的User类是实体类,在此本人不写了,请自行创建 User Entity。
server:
@PostMapping(value = "demoTest2", consumes = MediaType.APPLICATION_JSON_VALUE,produces = MediaType.APPLICATION_JSON_VALUE)
public String demoTest2(@RequestBody User user, @RequestHeader String phone){
System.out.println(user.getName());
System.out.println(user.getEmail());
System.out.println(phone);
return "success";
}
client:
@PostMapping("postTest1")
public void postTest1() {
String url = "http://IP:8080/demo/demoTest2";
HttpHeaders headers = new HttpHeaders();
headers.setContentType(MediaType.APPLICATION_JSON);
headers.set("phone", "123456");
Map<String, Object> params = new HashMap<>();
params.put("name", "yyc");
params.put("email", "12306");
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(params,headers);
ResponseEntity<String> request = restTemplate.postForEntity(url, httpEntity,String.class);
System.out.println(request.getBody());
}
当所有传递的数据类型为复杂数据类型: headers.setContentType(MediaType.MULTIPART_FORM_DATA);
并且需要接受的参数不再实体类中进行映射。
MultiValueMap来进行传递。
server:
@PostMapping(value = "demoTest3",produces = MediaType.APPLICATION_JSON_VALUE)
public String demoTest3(@RequestParam String name,String email, @RequestHeader String phone){
System.out.println(name);
System.out.println(email);
System.out.println(phone);
return "success";
}
client:
@PostMapping("postTest2")
public void postTest2() {
String url = "http://ip:8080/demo/demoTest3";
HttpHeaders headers = new HttpHeaders();
headers.set("phone", "1234567");
MultiValueMap<String, Object> params = new LinkedMultiValueMap<>();
params.add("name", "yyc");
params.add("email", "123067");
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(params, headers);
ResponseEntity<String> request = restTemplate.postForEntity(url, httpEntity, String.class);
System.out.println(request.getBody());
}
既有实体类,又有普通的参数
server:
@PostMapping(value = "demoTest4",produces = MediaType.APPLICATION_JSON_VALUE)
public String demoTest4(@RequestBody User user,String sex, @RequestHeader String phone){
System.out.println(user.getName());
System.out.println(user.getEmail());
System.out.println(sex);
System.out.println(phone);
return "success";
}
client:
@PostMapping("postTest3")
public void postTest3() {
String url = "http://ip:8080/demo/demoTest4?sex={sex}";
HttpHeaders headers = new HttpHeaders();
headers.set("phone", "1234567");
Map<String, Object> params = new HashMap<>();
params.put("name", "yyc");
params.put("email", "123067");
Map<String,Object> other = new HashMap<>();
other.put("sex","男");
RestTemplate restTemplate = new RestTemplate();
HttpEntity httpEntity = new HttpEntity(params, headers);
ResponseEntity<String> request = restTemplate.postForEntity(url, httpEntity, String.class,other);
System.out.println(request.getBody());
}