您的位置:首页 > 理论基础 > 计算机网络

RestTemplate 调用

2015-11-02 22:20 656 查看
RestTemplate它简化了与HTTP服务器通信,执行基于rest的原则。它处理HTTP连接,使应用程序代码提供的url(可能的模板变量)和提取结果。

RestTemplate提供了一系列调用spring mvc rest(或者说 spring rest webservice)接口 可以调用http请求的WebService,并将结果转换成相应的对象类型。

基本方法 常用方法 get

url 请求地址 responseType 返回值类型

getForEntity(url, responseType);

参数地址 url, 返回值类型 Class<T> responseType, 参数可变参数  可以是map 、Object... urlVariables

rest.getForEntity(url, responseType, urlVariables)

参数url 返回值类型 参数类型为map

getForEntity(url, responseType,  Map<K, V)

参数地址 url, 返回值类型 Class<T> responseType, 参数可变参数  可以是map 、Object... urlVariables

getForObject(url, responseType,  Map<K, V) 类似方法参数与getForEntity相同  只是返回值不同 看下例子

Controller 类容方法如下:

<span style="font-size:14px;">@Controller
public class TestRestTemplate {

/**
* get请求
* 返回参数自定义 这里返回什么参数客户端接收什么参数
* @return
*/
@RequestMapping(value="/restget/{param}/get/{red}",method=RequestMethod.GET)
@ResponseBody
public Map<String, Object> restGetRequest(@PathVariable String param,
@PathVariable Integer red){
Map<String, Object> map =new HashMap<String, Object>();
map.put("param", param);
map.put("red", red);
System.out.println(param);
return map;
}

/**
* 返回字符串
* @param param
* @param red
* @return
*/
@RequestMapping(value="/restpath/{param}/get/{red}",method=RequestMethod.GET)
@ResponseBody
public String rest(@PathVariable String param,
@PathVariable Integer red){
System.out.println(red);
return param+":"+red;
}
}</span>

看看客户端调用:
<span style="font-size:14px;">public class WebRestTemplate {

RestTemplate rest=new RestTemplate();

/**
* 调用getForEntity 实现get请求 看参数设置
* @return
*/
public Map<String, Object> getForEntity1(){
String url="http://localhost:8080/share-web/restget/www/get/2";
ResponseEntity<Map> map=rest.getForEntity(url, Map.class);
Map<String, Object> maps=map.getBody();
System.out.println(maps);
return maps;
}

/**
* rest.getForEntity方法 看参数设置 可变参数
* @return
*/
public Map<String, Object> getForEntity2(){
String url="http://localhost:8080/share-web/restget/{param}/get/{red}";
ResponseEntity<Map> map=rest.getForEntity(url, Map.class, "www",1);
Map<String, Object> maps=map.getBody();
return maps;
}

/**
* test 参数返回字符串
* map为参数
* @return
*/
public String getForEntity3(){
String url="http://localhost:8080/share-web/restpath/{param}/get/{red}";
Map<String, Object> m=new HashMap<String, Object>();
m.put("param", "test");
m.put("red", 3);
ResponseEntity<String> map=rest.getForEntity(url, String.class,m);
String str=map.getBody();
return str;
}

/**
* 有时候后我们需要设置头部信息 本人这次就上当了搞了好的半天
* 能用于基于HttpEntity 设置头
* exchange()方法可以用于添加请求和响应头
* @param url
* @param param
* @return
*/
public String getRequest(){
String url="http://localhost:8080/share-web/restpath/{param}/get/{red}";
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("head", "xxx");
Map<String, Object> m=new HashMap<String, Object>();
m.put("param", "test");
m.put("red", 3);
// requestHeaders.setAccept(acceptableMediaTypes);
// requestHeaders.setContentType(MediaType.APPLICATION_JSON);
HttpEntity<?> requestEntity = new HttpEntity(requestHeaders);
HttpEntity<String> response = rest.exchange(
url,
HttpMethod.GET, requestEntity, String.class, m);
String str=response.getBody();
return str;
}

public static void main(String[] args) {
WebRestTemplate web=new WebRestTemplate();

Map<String, Object> map=web.getForEntity1();
System.out.println(map);

Map<String, Object> maps=web.getForEntity2();
System.out.println(maps);

String str=web.getForEntity3();
System.out.println(str);

String str2=web.getRequest();
System.out.println(str2);
}
}</span>

接下来看看 post中的那些方法 常用的就这两种 其他的都一样

url地址 request请求参数【比如 这参数设置后面的参数 可以不用参数】 responsType 返回值类型 请求参数  

postForObject(url, request, responseType, uriVariables);

postForEntity(url, request, responseType, uriVariables);

Controller 类容方法如下:

<span style="font-size:14px;">@Controller
public class TestRestTemplate {

@RequestMapping(value="/restpost",method=RequestMethod.POST)
@ResponseBody
public Map<String, Object> restPost(@RequestBody Map<String,Object> map){

System.out.println(map);
map.put("aaa", "ffffff");
return map;
}
}</span>

看看客户端调用:
<span style="font-size:14px;">public class WebRestTemplate {

RestTemplate rest=new RestTemplate();

public Map<String, Object> postForEntity(){

String url="http://localhost:8080/share-web/restpost";
//设置head
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("head", "password");
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
Map<String, Object> m=new HashMap<String, Object>();
m.put("param", "test");
m.put("red", 3);
//通过HttpEntity 设置参数 head
HttpEntity<?> request=new HttpEntity(m, requestHeaders);
//url地址 request请求参数 responsType 返回值类型 请求参数
ResponseEntity<Map> entitymap= rest.postForEntity(url, request, Map.class);
Map<String, Object> map=entitymap.getBody();
return map;
}

public Map<String, Object> postObject(){

String url="http://localhost:8080/share-web/restpost";
//设置head
HttpHeaders requestHeaders = new HttpHeaders();
requestHeaders.set("head", "password");
requestHeaders.setContentType(MediaType.APPLICATION_JSON);
requestHeaders.setAccept(Collections.singletonList(MediaType.APPLICATION_JSON));
Map<String, Object> m=new HashMap<String, Object>();
m.put("param", "test");
m.put("red", 3);
//通过HttpEntity 设置参数 head
HttpEntity<?> request=new HttpEntity(m, requestHeaders);
//url地址 request请求参数 responsType 返回值类型 请求参数
Map<String, Object> mapobj= rest.postForObject(url, request, Map.class);
return mapobj;
}

public static void main(String[] args) {
WebRestTemplate web=new WebRestTemplate();

Map<String, Object> map=web.postObject();
System.out.println(map);

Map<String, Object> maps=web.postForEntity();
System.out.println(maps);
}
}</span>

搞了很久 终于搞定了  哈哈哈哈哈哈
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息