RestTemplate请求工具类

06-21 1017阅读

首先,配置 RestTemplate bean:

RestTemplate请求工具类
(图片来源网络,侵删)
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.client.RestTemplate;
@Configuration
public class RestTemplateConfig {
    @Bean
    public RestTemplate restTemplate() {
        return new RestTemplate();
    }
}
RestTemplateUtil 工具类

创建一个工具类来封装 RestTemplate 的使用:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Component;
import org.springframework.web.client.RestTemplate;
import java.util.Map;
@Component
public class RestTemplateUtil {
    @Autowired
    private RestTemplate restTemplate;
    // GET 请求
    public String sendGetRequest(String url, Map headers) {
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }
        HttpEntity entity = new HttpEntity(httpHeaders);
        ResponseEntity response = restTemplate.exchange(url, HttpMethod.GET, entity, String.class);
        return response.getBody();
    }
    // POST 请求
    public String sendPostRequest(String url, String json, Map headers) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "application/json");
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }
        HttpEntity entity = new HttpEntity(json, httpHeaders);
        ResponseEntity response = restTemplate.exchange(url, HttpMethod.POST, entity, String.class);
        return response.getBody();
    }
    // PUT 请求
    public String sendPutRequest(String url, String json, Map headers) {
        HttpHeaders httpHeaders = new HttpHeaders();
        httpHeaders.set("Content-Type", "application/json");
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }
        HttpEntity entity = new HttpEntity(json, httpHeaders);
        ResponseEntity response = restTemplate.exchange(url, HttpMethod.PUT, entity, String.class);
        return response.getBody();
    }
    // DELETE 请求
    public String sendDeleteRequest(String url, Map headers) {
        HttpHeaders httpHeaders = new HttpHeaders();
        if (headers != null) {
            headers.forEach(httpHeaders::add);
        }
        HttpEntity entity = new HttpEntity(httpHeaders);
        ResponseEntity response = restTemplate.exchange(url, HttpMethod.DELETE, entity, String.class);
        return response.getBody();
    }
}

使用 RestTemplateUtil 工具类

在你的服务或控制器类中,你可以这样使用 RestTemplateUtil:

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.HashMap;
import java.util.Map;
@Service
public class ApiService {
    @Autowired
    private RestTemplateUtil restTemplateUtil;
    public void performApiCalls() {
        String getUrl = "https://api.example.com/data";
        String postUrl = "https://api.example.com/update";
        String putUrl = "https://api.example.com/update/1";
        String deleteUrl = "https://api.example.com/delete/1";
        String json = "{\"key\":\"value\"}";
        // GET 请求示例
        String getResponse = restTemplateUtil.sendGetRequest(getUrl, null);
        System.out.println("GET Response: " + getResponse);
        // POST 请求示例
        Map postHeaders = new HashMap();
        postHeaders.put("Authorization", "Bearer token");
        String postResponse = restTemplateUtil.sendPostRequest(postUrl, json, postHeaders);
        System.out.println("POST Response: " + postResponse);
        // PUT 请求示例
        Map putHeaders = new HashMap();
        putHeaders.put("Authorization", "Bearer token");
        String putResponse = restTemplateUtil.sendPutRequest(putUrl, json, putHeaders);
        System.out.println("PUT Response: " + putResponse);
        // DELETE 请求示例
        Map deleteHeaders = new HashMap();
        deleteHeaders.put("Authorization", "Bearer token");
        String deleteResponse = restTemplateUtil.sendDeleteRequest(deleteUrl, deleteHeaders);
        System.out.println("DELETE Response: " + deleteResponse);
    }
}
VPS购买请点击我

文章版权声明:除非注明,否则均为主机测评原创文章,转载或复制请以超链接形式并注明出处。

目录[+]