restTemplete webService in spring
2016-04-21 15:11
465 查看
spring rest mvc使用RestTemplate远程接口调用
主要代码如下:
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) {
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);
}
}
可参考博客:
http://yangjizhong.iteye.com/blog/600540 http://yangjizhong.iteye.com/blog/600680
主要代码如下:
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) {
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);
}
}
可参考博客:
http://yangjizhong.iteye.com/blog/600540 http://yangjizhong.iteye.com/blog/600680
相关文章推荐
- eclipse新建安卓项目低于4.0,去除v7依赖包的方法
- (java)关于List中对象实现Comparable接口的例子
- java 面向对象及编程理解
- springMVC 枚举传值问题
- [置顶] JAVA串口通讯程序
- 《Spring实战》学习笔记-第六章:web视图解析
- Eclipse下调试安卓程序
- 《Spring实战》学习笔记-第五章:构建Spring web应用
- JAVA数据类型
- ERROR: JAVA_HOME is set to an invalid directory. JAVA_HOME = "D:\tools\jdk\jdk1.8.0_73" Please set t
- java 日期格式化
- [改善Java代码]不要只替换一个类
- java对cookie的操作
- SpringMVC
- java注解(annotation)
- Java的4种代码块总结
- SpringMVC注解@Autowired和@Qualifier 自动注入[根据类型注入] @Autowired 可以对成员变量、方法以及构造函数进行注释, @Qualifier 的
- Java Map和Bean相互转换
- Java Map和Bean相互转换
- Java 构造函数问题