springboot restTemplate 使用示例
**************************
示例
HelloController
@RestController
public class HelloController {
@RequestMapping("/hello")
public String hello(){
return "hello world";
}
@RequestMapping("/hello2/{name}")
public String hello2(@PathVariable("name") String name){
return name;
}
@RequestMapping("/hello3")
public Person hello3(String name,Integer age){
Person person=new Person();
person.setName(name);
person.setAge(age);
return person;
}
@RequestMapping("/hello4")
public Person hello4(@RequestBody Person person){
return person;
}
@RequestMapping("/hello5")
public String hello5(HttpServletRequest request){
Enumeration<String> headerNames=request.getHeaderNames();
while (headerNames.hasMoreElements()){
String name=headerNames.nextElement();
System.out.println(name+" ==> "+request.getHeader(name));
}
return "success";
}
}
Hello2Controller
@RestController
public class Hello2Controller {
@Resource
private RestTemplate restTemplate;
@RequestMapping("/test")
public String test(){
return restTemplate.getForObject("http://localhost:8080/hello",String.class);
}
@RequestMapping("/test2")
public String test2(){
return restTemplate.getForObject("http://localhost:8080/hello2/{name}",String.class,"瓜田李下");
}
@RequestMapping("/test3")
public String test3(){
Map<String,String> params=new HashMap<>();
params.put("name","瓜田李下");
return restTemplate.getForObject("http://localhost:8080/hello2/{name}",String.class,params);
}
@RequestMapping("/test4")
public Person test4(){
Map<String,String> params=new HashMap<>();
params.put("name","瓜田李下");
params.put("age","20");
return restTemplate.getForObject("http://localhost:8080/hello3?name={name}&age={age}",Person.class,params);
}
@RequestMapping("/test5")
public String test5(){
return restTemplate.postForObject("http://localhost:8080/hello2/{name}",null,String.class,"瓜田李下");
}
@RequestMapping("/test6")
public String test6(){
Map<String,String> params=new HashMap<>();
params.put("name","瓜田李下");
return restTemplate.postForObject("http://localhost:8080/hello2/{name}",null,String.class,params);
}
@RequestMapping("/test7")
public Person test7(){
Map<String,String> params=new HashMap<>();
params.put("name","瓜田李下");
params.put("age","20");
return restTemplate.postForObject("http://localhost:8080/hello3?name={name}&age={age}",null,Person.class,params);
}
@RequestMapping("/test8")
public Person test8(){
Map<String,String> params=new HashMap<>();
params.put("name","瓜田李下");
params.put("age","20");
HttpEntity<Map<String,String>> entity=new HttpEntity<>(params);
return restTemplate.postForObject("http://localhost:8080/hello4",entity,Person.class);
}
@RequestMapping("/test9")
public String test9(){
HttpHeaders headers=new HttpHeaders();
headers.add("custom","custom header");
HttpEntity<Object> httpEntity=new HttpEntity<>(headers);
return restTemplate.postForObject("http://localhost:8080/hello5",httpEntity,String.class);
}
}
**************************
使用测试
localhost:8080/test,输出:hello world
localhost:8080/test2,输出:瓜田李下
localhost:8080/test3,输出:瓜田李下
localhost:8080/test4,输出:
localhost:8080/test5,输出:瓜田李下
localhost:8080/test6,输出:瓜田李下
localhost:8080/test7,输出:
localhost:8080/test8,输出
localhost:8080/test9,控制台输出:
accept ==> text/plain, application/json, application/*+json, */*
custom ==> custom header
user-agent ==> Java/14.0.1
host ==> localhost:8080
connection ==> keep-alive
content-type ==> application/x-www-form-urlencoded
content-length ==> 0
自定义header添加成功