`
zhaoshijie
  • 浏览: 2242319 次
  • 性别: Icon_minigender_1
  • 来自: 北京
社区版块
存档分类
最新评论

spring rest mvc使用RestTemplate调用

 
阅读更多


关键字:RestTemplate使用




可参考博客:

http://yangjizhong.iteye.com/blog/600540

http://yangjizhong.iteye.com/blog/600680


附件是一个简单的工程,有get/post/delete/put等调用方式,请下载使用,希望能帮助大家...




主要代码如下:


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

import org.springframework.web.client.RestTemplate;

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

/**
* @param args
*/
public static void main(String[] args) {
RestTemplate restTemplate = new RestTemplate();  

//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);

}

}

分享到:
评论
5 楼 wangyudong 2018-03-14  
新版本 Wisdom RESTClient
https://github.com/Wisdom-Projects/rest-client
4 楼 wangyudong 2017-11-27  
很多API doc生成工具生成API文档需要引入第三方依赖,重度依赖在代码里加注解的方式,并且不支持自动化测试RESTful API。

之前习惯用一款名字为 WisdomTool REST Client,它能够基于测试过的历史记录自动生成精美的RESTful API文档。完全不用引入第三方依赖,不用在代码里添加任何注解。完全跟API代码脱离,任何用户即使不了解API代码,也能够使用WisdomTool REST Client生成API文档。并且支持自动化测试RESTful API,输出精美的测试报告。
轻量级的工具,功能却很精悍哦!

https://github.com/wisdomtool/rest-client

Most of API doc tools do not support automated testing.

Once used a tool called WisdomTool REST Client supports automatically generating exquisite RESTful API documentation based on history testing cases without adding annotations to the code, it also supports automated testing, and outputs exquisite report.

Lightweight tool with very powerful features!

https://github.com/wisdomtool/rest-client
3 楼 yanping158 2015-08-14  
服务端想在方法中接Map类型的参数怎么接呢?
2 楼 zhaoshijie 2015-05-15  
你得有服务器端接口  客户端修改为你调用的具体API
1 楼 menghuannvxia 2015-05-15  
您好,这个RestTemplate还需要配置吗

相关推荐

Global site tag (gtag.js) - Google Analytics