使用RestTemplate实现rest服务的调用
2017-08-25 00:00
841 查看
摘要: 使用spring的restTemplate模板实现远程服务调用。
创建rest工具类继承RestTemplate
调用服务:
添加maven依赖: <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency>
创建rest工具类继承RestTemplate
import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; import org.springframework.http.MediaType; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Component; import org.springframework.web.client.RestTemplate; import com.alibaba.fastjson.JSONObject; /** * Desc: * Author: 李阳 * mobile: 15002992382 * email:ly_triangle@126.com * Date: 2017/8/23 16:01 */ @Component public class ClientUtil extends RestTemplate { public JSONObject callGetMethod(String requestUrl) { HttpEntity entity = new HttpEntity(new HttpHeaders()); ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.GET, entity, String.class); return JSONObject.parseObject(result.getBody()); } public JSONObject callPostMethod(String requestUrl, String jsonBody) { HttpHeaders header = new HttpHeaders(); header.set("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8"); HttpEntity entity = new HttpEntity(jsonBody, header); ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.POST, entity, String.class); return JSONObject.parseObject(result.getBody()); } public JSONObject callPutMethod(String requestUrl, String jsonBody) { HttpHeaders header = new HttpHeaders(); header.set("Content-Type", MediaType.APPLICATION_JSON + ";charset=UTF-8"); HttpEntity entity = new HttpEntity(jsonBody, header); ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.PUT, entity, String.class); return JSONObject.parseObject(result.getBody()); } public JSONObject callDeleteMethod(String requestUrl) { HttpHeaders header = new HttpHeaders(); HttpEntity entity = new HttpEntity(header); ResponseEntity<String> result = this.exchange(requestUrl, HttpMethod.DELETE, entity, String.class); return JSONObject.parseObject(result.getBody()); }
调用服务:
import com.alibaba.fastjson.JSONObject; import com.utils.ClientUtil; import com.utils.HttpEntityUtil; import org.springframework.beans.factory.annotation.Autowired; importorg.springframework.http.HttpEntity; import org.springframework.http.HttpMethod; import org.springframework.http.ResponseEntity; import org.springframework.stereotype.Service; /** * Author: 李阳 * Date: 16/08/2017 8:54 PM * Desc: Rest服务调用 */ @Service public class RestServiceInvoking { @Autowired private ClientUtil clientUtil; public JSONObject loadSth(Long id) { String requestUrl = tmisServiceUrl + "/sth/" + id; ResponseEntity<String> result = clientUtil.callGetMethod(requestUrl); return JSONObject.parseObject(result.getBody()); } public JSONObject addSth(String view) { String requestUrl = tmisServiceUrl + "/sth"; ResponseEntity<String> result = clientUtil.callPostMethod(requestUrl,view); return JSONObject.parseObject(result.getBody()); } public JSONObject updateSth(String view) { String requestUrl = tmisServiceUrl + "/sth"; ResponseEntity<String> result = clientUtil.callPutMethod(requestUrl,view); return JSONObject.parseObject(result.getBody()); } public JSONObject deleteSth(Long id) { String requestUrl = tmisServiceUrl + "/sth/" + id; ResponseEntity<String> result = clientUtil.callDeleteMethod(requestUrl); return JSONObject.parseObject(result.getBody()); } }
相关文章推荐
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- SpringCloud使用eureka(rest)和ribbon实现服务调用和负载均衡
- SpringCloud Cloud查找调用REST服务使用RestTemplate(ribbon负载) 或feign模式火推13
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- SpringCloud 查找调用REST服务使用RestTemplate(ribbon负载)或feign模式 教程源码 火推
- 使用RestTemplate来构建远程调用服务
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 搭建rtmp直播流服务之2:使用java实现ffmpeg命令接口化调用(用java执行ffmpeg命令)
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【五】——在Web Api中实现Http方法(Put,Post,Delete)
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【六】——实现资源间的关联
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【七】——实现资源的分页
- spring rest mvc使用RestTemplate调用
- ArcGIS API for javascript开发笔记(六)——REST详解及如何使用REST API调用GP服务
- ArcGIS API for javascript开发笔记(六)——REST详解及如何使用REST API调用GP服务
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【五】——在Web Api中实现Http方法(Put,Post,Delete)
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【七】——实现资源的分页
- 使用ASP.NET Web Api构建基于REST风格的服务实战系列教程【四】——实现模型工厂,依赖注入以及格式配置
- 使用Wsgi实现web服务的调用