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");
    
  • 相关阅读:
    WRF WPS预处理
    CVS安装
    Linux窗口最小化消失,任务栏上无法找到的解决方法
    NCARG安装配置出现error while loading shared libraries: libg2c.so.0问题额解决办法
    Netcdf安装
    Embedding R-generated Interactive HTML pages in MS PowerPoint(转)
    The leaflet package for online mapping in R(转)
    Some 3D Graphics (rgl) for Classification with Splines and Logistic Regression (from The Elements of Statistical Learning)(转)
    What does a Bayes factor feel like?(转)
    Weka算法介绍
  • 原文地址:https://www.cnblogs.com/SamNicole1809/p/12610533.html
Copyright © 2011-2022 走看看