zoukankan      html  css  js  c++  java
  • spring restTemplate 进行http请求的工具类封装

    本文为博主原创,未经允许不得转载:

      1.对常用调用的方法进行封装:

    import org.springframework.http.HttpHeaders;
    
    import com.alibaba.fastjson.JSONObject;
    
    
    public interface SpringRestService {
    
        public <T> T postWithJson(String url, JSONObject param, Class<T> responseType);
    
        public <T> T postWithJson(String url, HttpHeaders headers, JSONObject param, Class<T> responseType);
    
        public <T> T postWithJson(String url, HttpHeaders headers, String param, Class<T> responseType);
    
        public <T> T getForObject(String url, Class<T> responseType);
    
        public String getForObject(String url, HttpHeaders headers);
    
        public String getForXml(String url, HttpHeaders headers);
    
    }

      2.对上面接口实现封装:

    import org.slf4j.Logger;
    import org.slf4j.LoggerFactory;
    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.MediaType;
    import org.springframework.http.ResponseEntity;
    import org.springframework.stereotype.Service;
    import org.springframework.web.client.RestTemplate;
    
    import com.alibaba.fastjson.JSONObject;
    import com.intf.service.springrest.SpringRestService;
    
    
    @Service("springRestService")
    public class SpringRestServiceImpl implements SpringRestService {
    
        @Autowired
        private RestTemplate restTemplate;
        
        private static final Logger LOGGER = LoggerFactory.getLogger(SpringRestServiceImpl.class);
    
        @Override
        public <T> T postWithJson(String url, JSONObject param, Class<T> responseType) {
            // 设置请求头
            HttpHeaders headers = new HttpHeaders();
            // 设置ContentType
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            // 请求
            return postWithJson(url, headers, param, responseType);
        }
    
        @Override
        public <T> T postWithJson(String url, HttpHeaders headers, JSONObject param, Class<T> responseType) {
            // 请求
            return postWithJson(url, headers, param.toString(), responseType);
        }
    
        @Override
        public <T> T postWithJson(String url, HttpHeaders headers, String param, Class<T> responseType) {
            // 设置ContentType
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            // 请求中设置param和headers
            HttpEntity<String> request = new HttpEntity<>(param, headers);
            // post请求并返回
            return restTemplate.postForObject(url, request, responseType);
        }
    
        @Override
        public <T> T getForObject(String url, Class<T> responseType) {
            // get请求并返回
            return restTemplate.getForObject(url, responseType);
        }
    
        @Override
        public String getForObject(String url, HttpHeaders headers) {
            // 设置ContentType
            headers.setContentType(MediaType.APPLICATION_JSON_UTF8);
            // 设置请求头
            HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
            // get请求获取响应内容
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
            // 返回响应body
            return response.getBody();
        }
    
        @Override
        public String getForXml(String url, HttpHeaders headers) {
            // 设置ACCEPT
            headers.add("Accept", MediaType.APPLICATION_XML_VALUE);
            // 设置ContentType
            headers.setContentType(MediaType.APPLICATION_XML);
            // 设置请求头
            HttpEntity<String> requestEntity = new HttpEntity<>(null, headers);
            // get请求获取响应内容
            ResponseEntity<String> response = restTemplate.exchange(url, HttpMethod.GET, requestEntity, String.class);
            // 返回响应body
            LOGGER.info(url + "&&&&&" + response.toString() + "&&&&&" + response.getBody());
            return response.getBody();
        }
    }
  • 相关阅读:
    投资银行的IT部门——不同之处与常见误解
    C++ error C2440: “类型转换” : 无法从“std::vector::iterator”转换为“
    查看端口占用
    Sc config http start= disabled
    DDL、DML和DCL的区别与理解
    不同网段,在路由器上如何设置网关
    服务器调优
    查看SQL Server版本信息
    基于32位Windows2003的数据库服务器优化,启用AWE,优化SQL Server
    在WIN7操作系统下,如何显示文件夹里文件的数目
  • 原文地址:https://www.cnblogs.com/zjdxr-up/p/14221069.html
Copyright © 2011-2022 走看看