SpringBoot-RestTemplate实现调用第三方API
2018-07-23 18:33
573 查看
1. RestTemplate的方式来调用别人的API,将数据转化为json 格式,引入了fastjson
[code]<dependency> <groupId>com.alibaba</groupId> <artifactId>fastjson</artifactId> <version>1.2.28</version> </dependency>
2. 编写RestTemlateConfig,配置好相关信息
[code] import org.springframework.context.annotation.Bean; import org.springframework.context.annotation.Configuration; import org.springframework.http.client.ClientHttpRequestFactory; import org.springframework.http.client.SimpleClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; @Configuration public class RestTemplateConfig { @Bean public RestTemplate restTemplate(ClientHttpRequestFactory factory){ return new RestTemplate(factory); } @Bean public ClientHttpRequestFactory simpleClientHttpRequestFactory(){ SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory(); factory.setConnectTimeout(15000); factory.setReadTimeout(5000); return factory; } }
3.编写controller,调用第三方的API,浏览器模拟get请求,postman模拟post请求
[code] import com.alibaba.fastjson.JSONObject; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.http.*; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import org.springframework.web.client.RestTemplate; import java.util.Map; @RestController public class SpringRestTemplateController { @Autowired private RestTemplate restTemplate; /***********HTTP GET method*************/ @GetMapping("/testGetApi") public String getJson(){ String url="http://localhost:8089/o2o/getshopbyid?shopId=19"; //String json =restTemplate.getForObject(url,Object.class); ResponseEntity<String> results = restTemplate.exchange(url, HttpMethod.GET, null, String.class); String json = results.getBody(); return json; } /**********HTTP POST method**************/ @PostMapping(value = "/testPost") public Object postJson(@RequestBody JSONObject param) { System.out.println(param.toJSONString()); param.put("action", "post"); param.put("username", "tester"); param.put("pwd", "123456748"); return param; } @PostMapping(value = "/testPostApi") public Object testPost() { String url = "http://localhost:8081/girl/testPost"; JSONObject postData = new JSONObject(); postData.put("descp", "request for post"); JSONObject json = restTemplate.postForEntity(url, postData, JSONObject.class).getBody(); return json; } }
阅读更多
相关文章推荐
- Spring中使用RestTemplate调用第三方接口API
- SpringBoot系列十一:SpringBoot整合Restful架构(使用 RestTemplate 模版实现 Rest 服务调用、Swagger 集成、动态修改日志级别)
- SpringBoot 使用RestTemplate实现调用服务
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- 如何使用Spring Boot,Spring Data和H2 DB实现REST API
- SpringBoot通过restTemplate实现消费服务
- 【SpringBoot】RestTemplate调用报错:'org.springframework.web.client.RestTemplate' that could not be found.
- springboot中用RestTemplate调用百度ip定位服务
- spring boot / cloud (八) 使用RestTemplate来构建远程调用服务
- Spring Boot REST API错误处理指南
- SpringBoot Rest-api开发
- android调用第三方api实现用户数授权登录机制详解
- Spring Boot中使用@Async实现异步调用
- SpringBoot使用多线程实现异步调用
- 使用 Spring RestTemplate 调用 rest 服务时自定义请求头(custom HTTP headers)
- Spring Boot + Dubbo 可运行的例子源码-实现服务注册和远程调用
- python调用百度REST API实现语音识别
- springboot restTemplate使用以及相关配置
- spring boot RestTemplate 发送 get 请求使用误区
- Java 不使用第三方类库实现REST调用