加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 综合聚焦 > 服务器 > 安全 > 正文

restTemplete webService in spring

发布时间:2020-12-16 22:17:10 所属栏目:安全 来源:网络整理
导读:spring rest mvc使用RestTemplate远程接口调用 主要代码如下:? import java.util.HashMap;? import java.util.Map;? import org.springframework.web.client.RestTemplate;? /**? * RestTemplate提供了一系列调用spring mvc rest(或者说 spring rest webse
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,80);?
// String message3 = restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate",null,urlVariables);?

//直接使用URL传递参数?
// String message = restTemplate.postForObject("http://localhost:8080/yongbarservice/appstore/appgoods/restTemplate?name=zhaoshijie&id=80",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?

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读