zoukankan      html  css  js  c++  java
  • springboot 请求外部接口方法

    直接上代码:

    1、定义配置类

    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 HttpApiConfig {
        @Bean
        public RestTemplate restTemplate(ClientHttpRequestFactory factory) {
            return new RestTemplate(factory);
        }
    
        @Bean
        public ClientHttpRequestFactory simpleClientHttpRequestFactory() {
            SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();
            factory.setReadTimeout(5000);
            factory.setConnectTimeout(5000);
            return factory;
        }
    }
    

    2、定义服务

    import org.springframework.http.HttpEntity;
    import org.springframework.http.HttpHeaders;
    import org.springframework.http.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Service;
    import org.springframework.util.MultiValueMap;
    import org.springframework.web.client.RestTemplate;
    
    import java.util.Map;
    
    @Service
    public class HttpUtils {
    
        private final RestTemplate restTemplate;
    
        public HttpUtils(RestTemplate restTemplate) {
            this.restTemplate = restTemplate;
        }
    
        // Get请求
        public String Get(String url, Map<String, Object> params) {
            String uri = buildUri(url, params);
            ResponseEntity<String> response = restTemplate.getForEntity(uri, String.class);
            return response.getBody();
        }
    
        // Post请求(JSON请求头)
        public String JPost(String url, Map<String, Object> params) {
            HttpEntity<Map<String, Object>> request = new HttpEntity<>(params, jsonHeaderBuilder());
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
            return response.getBody();
        }
    
        // Post请求(URL请求头)
        public String UPost(String url, MultiValueMap<String, String> params) {
            HttpEntity<MultiValueMap<String, String>> request = new HttpEntity<>(params, urlHeaderBuilder());
            ResponseEntity<String> response = restTemplate.postForEntity(url, request, String.class);
            return response.getBody();
        }
    
        // 拼接Url和参数
        private String buildUri(String url, Map<String, Object> params) {
            StringBuilder sb = new StringBuilder(url);
            sb.append("?");
            for (Map.Entry<String, Object> entry : params.entrySet()) {
                sb.append(entry.getKey()).append("=").append(entry.getValue()).append("&");
            }
            sb.deleteCharAt(sb.length() - 1);
            return sb.toString();
        }
    
        // 构建Url请求头
        private HttpHeaders urlHeaderBuilder() {
            HttpHeaders h = new HttpHeaders();
            h.setContentType(MediaType.APPLICATION_FORM_URLENCODED);
            return h;
        }
    
        // 构建Json请求头
        private HttpHeaders jsonHeaderBuilder() {
            HttpHeaders h = new HttpHeaders();
            h.setContentType(MediaType.APPLICATION_JSON);
            return h;
        }
    }
    

    3、实例(参考)

    POST请求,参数带在URL上

    MultiValueMap<String, String> requestPara = new LinkedMultiValueMap<>();
    requestPara.add("id", id);
    requestPara.add("name", name);
    StringBuilder sb = new StringBuilder(baseUrl);
    sb.append("/oh/yeah");
    String response = httpUtils.UPost(sb.toString(), requestPara);
    

    GET和JPOST请求是常用的,参数添加与UPost类似,类型变一下。

    Map<String, Object> requestPara = new HashMap<>();
    requestPara.put("id", id);
    requestPara.put("name", name);
    StringBuilder sb = new StringBuilder(baseUrl);
    sb.append("/oh/yeah");
    
  • 相关阅读:
    ssh登录很慢的问题
    Y480&Y580 刷slic2.1全自动教程
    re正则表达式5_*
    linux下查看内存使用情况
    检查linux网络的状况
    Linux Load average负载详细解释
    查看Linux磁盘空间大小
    Linux 批量重命名文件
    Linux 网卡丢包严重
    linux 下vi /vim 中文汉字乱码解决
  • 原文地址:https://www.cnblogs.com/SamNicole1809/p/12610533.html
Copyright © 2011-2022 走看看