SpringBoot使用RestTemplate调用第三方接口
创建项目结构
![在这里插入图片描述](https://img-blog.csdnimg.cn/c50c2dcef61e4f218f971e2145d06497.png)
pom文件修改
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>2.7.11</version>
<relativePath/>
</parent>
<groupId>com.example</groupId>
<artifactId>demo1</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>demo1</name>
<description>demo1</description>
<properties>
<java.version>1.8</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
<spring-boot.version>2.3.7.RELEASE</spring-boot.version>
<fastjson.version>1.2.47</fastjson.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
</dependency>
<dependency>
<groupId>com.alibaba</groupId>
<artifactId>fastjson</artifactId>
<version>${fastjson.version}</version>
</dependency>
<dependency>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
<optional>true</optional>
</dependency>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
<configuration>
<excludes>
<exclude>
<groupId>org.projectlombok</groupId>
<artifactId>lombok</artifactId>
</exclude>
</excludes>
</configuration>
</plugin>
</plugins>
</build>
</project>
config文件
package com.example.demo1.config;
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;
}
}
controller文件
package com.example.demo1.controller;
import com.example.demo1.proxy.ARestTemplate;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/a")
public class AController {
@Autowired
private ARestTemplate aRestTemplate;
@GetMapping("/doGetEnity1")
public String doGetEnity1(){
try{
String result= aRestTemplate.doGetEnity1("http://www...?loginName=...&password=...");
System.out.println(result);
return result;
}catch (Exception e){
return e.getMessage();
}
}
@GetMapping("/doGetForObj1")
public String doGetForObj1(){
try{
String result= aRestTemplate.doGetForObj1("http://www...?loginName=...&password=...");
System.out.println(result);
return result;
}catch (Exception e){
return e.getMessage();
}
}
@PostMapping("/doPostForEnity1")
public String doPostForEnity1(){
try{
String result= aRestTemplate.doPostForEnity1("https:/***/Save");
System.out.println("jsonobject:"+result);
return result;
}catch (Exception e){
return e.getMessage();
}
}
@PostMapping("/doPostForObj1")
public String doPostForObj1(){
try {
String result= aRestTemplate.doPostForObj1("https:/***/Save");
System.out.println("jsonobject:"+result);
return result;
} catch (Exception e) {
return e.getMessage();
}
}
@PostMapping("/doExchange")
public String doExchange(){
try {
String result = aRestTemplate.doExchange("https:/***/Save");
System.out.println("jsonobject:" + result);
return result;
} catch (Exception e) {
return e.getMessage();
}
}
}
proxy文件
package com.example.demo1.proxy;
import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.*;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;
import java.util.HashMap;
import java.util.Map;
@Service
public class ARestTemplate {
@Autowired
private RestTemplate restTemplate;
public String doGetEnity1(String url){
ResponseEntity<String> responseEntity = restTemplate.getForEntity(url, String.class);
String riskmerchantself = responseEntity.getBody();
return riskmerchantself;
}
public String doGetForObj1(String url){
String riskmerchantself = restTemplate.getForObject(url, String.class);
return riskmerchantself;
}
public String doPostForEnity1(String url){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json;charset=utf-8");
Map<String, Object> postData = new HashMap<>();
postData.put("merchant_name", "model1");
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(postData, httpHeaders);
ResponseEntity<String> responseEntity = restTemplate.postForEntity(url, httpEntity, String.class);
String body = responseEntity.getBody();
return body;
}
public String doPostForObj1(String url){
HttpHeaders httpHeaders = new HttpHeaders();
httpHeaders.add("Content-Type", "application/json;charset=utf-8");
Map<String, Object> postData = new HashMap<>();
postData.put("merchant_name", "model1");
HttpEntity<Map<String, Object>> httpEntity = new HttpEntity<>(postData, httpHeaders);
String body = restTemplate.postForObject(url, httpEntity, String.class);
return body;
}
public String doExchange(String url){
HttpHeaders headers = new HttpHeaders();
String token = "qqqqq";
headers.add("authorization", token);
headers.setContentType(MediaType.APPLICATION_JSON);
JSONObject obj = new JSONObject();
obj.put("merchant_name", "model exchange");
HttpEntity<JSONObject> request = new HttpEntity<>(obj, headers);
ResponseEntity<String> responseEntity = restTemplate.exchange(url, HttpMethod.POST, request, String.class);
String body = responseEntity.getBody();
return body;
}
}