spring rest mvc使用RestTemplate远程接口调用

匿名 (未验证) 提交于 2019-12-02 23:43:01


主要代码如下:

import java.util.HashMap;
import java.util.Map;

import org.springframework.web.client.RestTemplate;

/**
* RestTemplate提供了一系列调用spring mvc rest(或者说 spring rest webservice)接口
* 包括 get/post/delete/put/
*
*/
public class Resttemplate {

/**
* @param args
*/
public static void main(String[] args) {


//get方式***********************************************************************************************************

// //参数直接放在URL中
// String message = restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80", String.class );
//
//
// //参数使用MAP传递
// Map<String ,Object> urlVariables = new HashMap<String ,Object>();
// urlVariables.put("name", "zhaoshijie");
// urlVariables.put("id", 80);
// String message2 = restTemplate.getForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate", String.class, urlVariables);



//delete方式***********************************************************************************************************

//delete方法(注意:delete方法没有返回值,说明,id=0这个参数在服务器端可以不定义该参数,直接使用request获取)
// restTemplate.delete("http://localhost:8080/yongbarservice/appstore/appgoods/deleteranking?id=0");




//post方式***********************************************************************************************************
//使用MAP传递参数
// Map<String ,Object> urlVariables = new HashMap<String ,Object>();
// urlVariables.put("name", "zhaoshijie");
// urlVariables.put("id", 80);
// String message3 = restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate",null, String.class, urlVariables);

//直接使用URL传递参数
// String message = restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",null, String.class );



//put方式***********************************************************************************************************
//注意:delete方法没有返回值,说明,id=0这个参数在服务器端可以不定义该参数,直接使用request获取
restTemplate.put("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80" ,null);



// System.out.println(message);
// System.out.println(message2);
// System.out.println(message3);

}







转载于:https://my.oschina.net/usenrong/blog/197855

文章来源: https://blog.csdn.net/weixin_34293141/article/details/92028969
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!