springboot中配置restTemplate及简单使用
来源:互联网 发布:淘宝注册页面html模板 编辑:程序博客网 时间:2024/06/05 21:09
一、引入web的starter
确保classpath中含有
compile("org.springframework.boot:spring-boot-starter-web")
二、配置restTemplate的Bean
@Bean public RestTemplate restTemplate() { RestTemplateBuilder restTemplateBuilder = new RestTemplateBuilder(); return restTemplateBuilder.build(); }
三、自己简单封装的工具类
package com.kingboy.common.utils.restTemplate;import org.springframework.beans.factory.annotation.Autowired;import org.springframework.http.HttpEntity;import org.springframework.http.HttpHeaders;import org.springframework.http.MediaType;import org.springframework.stereotype.Component;import org.springframework.web.client.RestTemplate;import javax.annotation.Resource;import java.util.Map;/** * @Author kingboy * @Date 2017/7/21 下午3:19 * @Description RestClient is used to http请求 */@Componentpublic class RestKingClient { @Resource RestTemplate restTemplate; /** * get * @param url 请求地址 * @param param 参数 * @param returnClass 返回类型 * @return */ public <T> T get(String url, Class<T> returnClass, Map<String, ?> param) { return restTemplate.getForObject(url, returnClass, param); } /** * post * @param url 请求地址 * @param param 参数 * @param returnClass 返回类型 * @param header 自定义的头信息 * @return */ public <E> E post(String url, E param, Class<E> returnClass, Map<String, String> header) { HttpHeaders headers = new HttpHeaders(); header.forEach((o1, o2) -> headers.set(o1, o2)); HttpEntity<E> httpEntity = new HttpEntity<E>(param,headers); return restTemplate.postForObject(url, httpEntity, returnClass); } /** * post * @param url 请求地址 * @param param 参数 * @param returnClass 返回类型 * @return */ public <E> E postByDefault(String url, E param, Class<E> returnClass) { HttpHeaders headers = new HttpHeaders(); headers.setContentType(MediaType.APPLICATION_JSON); headers.set("Accept", "application/json"); HttpEntity<E> httpEntity = new HttpEntity<E>(param,headers); return restTemplate.postForObject(url, httpEntity, returnClass); }}
四、使用示例模拟
public class User { private String id; private String telephone; private String password; //setter,getter}
package com.kingboy.sso.controller.rest;import com.kingboy.common.utils.apiresult.ApiResult;import com.kingboy.common.utils.restTemplate.RestKingClient;import com.kingboy.ssoservice.domain.user.User;import org.springframework.web.bind.annotation.*;import javax.annotation.Resource;import java.util.HashMap;import java.util.Map;/** * @Author kingboy * @Date 2017/7/21 下午2:04 * @Description RestTemplateController is used to 测试RestTemplate */@RestController@RequestMapping("/Rest")public class RestTemplateController { @Resource private RestKingClient restKingClient; /** * 通过Get提供信息 * @return */ @GetMapping("/GetUser") public User getUserForRest() { //模拟假数据 return new User(1, "telephone", "password"); } /** * 测试Get方式 * @param telephone * @param password * @return */ @GetMapping("/GetUserFromRest/{telephone}/{password}") public ApiResult getUserByRest(@PathVariable String telephone, @PathVariable String password) { Map<String, String> param = new HashMap<>(2); param.put("telephone", telephone); param.put("password", password); //参数仅是演示用途 User user = restKingClient.get("http://localhost:8080/Rest/GetUser", User.class, param); return ApiResult.success(user); } /** * 通过POST提供信息 * @return */ @PostMapping("/PostUser") public User postUserForRest(@RequestBody User user,@RequestHeader String Token) { //模拟假数据 return new User(1, user.getTelephone(), user.getPassword()); } /** * 测试POST方式 * @param telephone * @param password * @return */ @GetMapping("/PostUserFromRest/{telephone}/{password}") public ApiResult postUserByRest(@PathVariable String telephone, @PathVariable String password) { //设置头信息 Map<String, String> headers = new HashMap<>(); headers.put("ContentType", "application/json"); headers.put("Token", "mytoken"); //设置Body User param = new User(null, telephone, password); User user = restKingClient.post("http://localhost:8080/Rest/PostUser", param, User.class,headers); return ApiResult.success(user); }}
阅读全文
0 0
- springboot中配置restTemplate及简单使用
- springboot中使用RestTemplate
- Springboot 使用 RestTemplate
- spring/springboot RestTemplate使用笔记
- 配置简单RestTemplate
- springboot-restTemplate
- springboot 使用RestTemplate 使用FastJson处理结果
- SpringBoot中,RestTemplate中文乱码解决方案
- springboot 使用及配置servlet
- SpringBoot中简单使用mongodb
- Spring中RestTemplate使用例子
- springboot RestTemplate 注入
- SpringBoot RestTemplate源码分析
- restTemplate使用及中文乱码问题
- Springmvc RestTemplate 使用 && json xml rest 配置
- SpringBoot中MongoDB注解概念及使用
- SpringBoot中配置使用Druid数据库连接池
- springboot中使用Mybatis注解配置详解
- Win32串行通信中文版(Serial Communications In Win32)
- HDU5858 Hard problem
- RabbitMQ--RabbitMQ-C客户端使用说明
- 使用jQuery的一些注意事项总结(持续更新......)
- 数据挖掘流程
- springboot中配置restTemplate及简单使用
- Python入门系列——第1篇
- 计算机网络基础学习
- 谓词的使用(NSPredicate)
- regular expression matching 正则匹配
- JVM菜鸟进阶高手之路二(JVM的重要性,Xmn是跟请求量有关。)
- 代理模式
- hdu1814 Peaceful Commission(2-sat)
- Emag eht htiw Em Pleh