SpringBoot 访问外部接口--工具类

前言

在实际的项目开发中,经常需要用到在自己的项目中调用第三方的接口,比如服务商这类的,然后再将传回的数据进行处理。而我现在所实习的公司用的是比较原生的Http请求方式,实现起来比较复杂。后来发现了现在比较常用的是利用HTTPClient库,可以较简单地实现调用第三方接口。接下来介绍怎么利用HTTPClient实现第三方接口的请求调用。

一、依赖导入

<!-- http通信 -->
<dependency>
    <groupId>org.apache.httpcomponents</groupId>
    <artifactId>httpclient</artifactId>
    <version>4.5.4</version>
</dependency>

<!-- 解析json -->
<dependency>
    <groupId>com.alibaba</groupId>
    <artifactId>fastjson</artifactId>
    <version>1.2.62</version>
</dependency>

注:httpclient库用来进行http通信,而我额外用阿里的fastjson来实现json数据的格式转换,类似此类的库还有Gson。

二、封装统一返回的数据格式

package com.chen.mykinthtest.restful;

import com.chen.mykinthtest.domain.AppConstant;

import java.util.HashMap;
import java.util.List;

/**
 * REST 接口返回数据
 *
 * @author chen
 */
public class RestResponse extends HashMap<String, Object> {

    /**
     * 禁止通过构造函数构造对象,只能通过静态方法获取实例。
     *
     * @see #ok()
     * @see #ok(String)
     * @see #fail()
     * @see #fail(String)
     */
    private RestResponse() {
    }

    /**
     * 设置接口返回的文本消息,属性 key: message
     *
     * @param msg
     * @return
     */
    public RestResponse msg(String msg) {
        this.put(AppConstant.MESSAGE, msg);
        return this;
    }

    /**
     * 设置接口返回的数据对象,属性 key: item
     *
     * @param item
     * @return
     */
    public RestResponse item(Object item) {
        this.put(AppConstant.ITEM, item);
        return this;
    }

    /**
     * 设置接口返回的数据对象列表,属性 key: list
     *
     * @param list
     * @return
     */
    public RestResponse list(List<?> list) {
        this.put(AppConstant.LIST, list);
        return this;
    }

    /**
     * 设置接口返回的数据项,并指定数据项的属性 key
     *
     * @param key
     * @param value
     * @return
     */
    public RestResponse put(String key, Object value) {
        super.put(key, value);
        return this;
    }

    /**
     * 接口执行成功的返回数据,其中属性 error = 0
     *
     * @return
     */
    public static RestResponse ok() {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK);
        return result;
    }

    /**
     * 接口执行成功的返回数据,并设置文本消息
     *
     * @param msg
     * @return
     */
    public static RestResponse ok(String msg) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK).msg(msg);
        return result;
    }

    /**
     * 接口执行成功的返回数据,并设置对象数据
     *
     * @param item
     * @return
     */
    public static RestResponse ok(Object item) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK).item(item);
        return result;
    }

    /**
     * 接口执行成功的返回数据,并设置列表对象数据
     *
     * @param list
     * @return
     */
    public static RestResponse ok(List<?> list) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.OK).list(list);
        return result;
    }

    /**
     * 接口执行失败的返回数据,其中属性 error = 1
     *
     * @return
     */
    public static RestResponse fail() {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.FAIL);
        return result;
    }

    /**
     * 接口执行失败的返回数据,并设置文本消息,其中属性 error = 1, message = {msg}
     *
     * @param msg
     * @return
     */
    public static RestResponse fail(String msg) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, AppConstant.FAIL).msg(msg);
        return result;
    }

    /**
     * 接口执行失败的返回数据,自定义状态码,其中属性 error = {errcode}
     *
     * @param errcode
     * @return
     */
    public static RestResponse fail(int errcode) {
        RestResponse result = new RestResponse();
        result.put(AppConstant.ERROR, errcode);
        return result;
    }
}

三、对应的常量库

注:可以选择不用,这是我自己的个人习惯。

package com.chen.mykinthtest.domain;

/**
 * 对常量进行封装,利于后期代码的维护
 *
 * @author chen
 */
public class AppConstant {

    // 文本消息
    public static final String MESSAGE = "message";

    // 单个对象
    public static final String ITEM = "item";

    // 返回的对象列表
    public static final String LIST = "list";

    // 状态码
    public static final String ERROR = "error";

    // 代表执行成功
    public static int OK = 0;

    // 代表执行失败
    public static int FAIL = 1;

    // 代表服务器运行异常
    public static int RunTime = 2;

    // 代表空指针异常
    public static int NullPointer = 3;

    // 类型转换异常
    public static int ClassCast = 4;

    // IO异常
    public static int IO = 5;

    // 未知方法异常
    public static int NoSuchMethod = 6;

    // 数组越界异常
    public static int IndexOutOfBounds = 7;

    // 400错误
    public static int HttpMessageNotReadable=8;

    // 400错误
    public static int TypeMismatch=9;

    // 400错误
    public static int MissingServletRequestParameter=10;

    // 405错误
    public static int HttpRequestMethodNotSupported=11;

    // 406错误
    public static int HttpMediaTypeNotAcceptable=12;

    // 500错误
    public static int Run500=13;

    // 栈溢出
    public static int StackOverflow=14;

    // 除数为0异常
    public static int Arithmetic=15;

    // 其他异常
    public static int other=16;

}

四、封装通信类

package com.chen.mykinthtest.service;

import org.apache.http.HttpStatus;
import org.apache.http.NameValuePair;
import org.apache.http.client.ClientProtocolException;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpGet;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.ContentType;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.message.BasicNameValuePair;
import org.apache.http.util.EntityUtils;

import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Http通信工具类
 *
 * @author chen
 */
public class HttpUtil {

    /**
     * post请求传输map数据
     *
     * @param url url地址
     * @param map map数据
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String sendPostDataByMap(String url, Map<String, Object> map) throws ClientProtocolException, IOException {
        String result = "";
        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);
        // 装填参数
        List<NameValuePair> nameValuePairs = new ArrayList<NameValuePair>();
        if (map != null) {
            for (Map.Entry<String, Object> entry : map.entrySet()) {
                nameValuePairs.add(new BasicNameValuePair(entry.getKey(), String.valueOf(entry.getValue())));
            }
        }

        // 设置参数到请求对象中
        httpPost.setEntity(new UrlEncodedFormEntity(nameValuePairs, "utf-8"));

        // 设置header信息
        // 指定报文头【Content-type】、【User-Agent】
        httpPost.setHeader("Content-type", "application/x-www-form-urlencoded");
        httpPost.setHeader("User-Agent", "Mozilla/4.0 (compatible; MSIE 5.0; Windows NT; DigExt)");

        // 执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = httpClient.execute(httpPost);
        // 获取结果实体
        // 判断网络连接状态码是否正常(0--200都是正常)
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            result = EntityUtils.toString(response.getEntity(), "utf-8");
        }
        // 释放连接
        response.close();

        return result;
    }

    /**
     * post请求传输json数据
     *
     * @param url  url地址
     * @param json json数据
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String sendPostDataByJson(String url, String json) throws ClientProtocolException, IOException {
        String result = "";

        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();
        // 创建post方式请求对象
        HttpPost httpPost = new HttpPost(url);
        // 设置参数到请求对象中
        StringEntity stringEntity = new StringEntity(json, ContentType.APPLICATION_JSON);
        stringEntity.setContentEncoding("utf-8");
        httpPost.setEntity(stringEntity);

        // 执行请求操作,并拿到结果(同步阻塞)
        CloseableHttpResponse response = httpClient.execute(httpPost);

        // 获取结果实体
        // 判断网络连接状态码是否正常(0--200都是正常)
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            result = EntityUtils.toString(response.getEntity(), "utf-8");
        }
        // 释放链接
        response.close();

        return result;
    }

    /**
     * get请求传输数据
     *
     * @param url
     * @return
     * @throws ClientProtocolException
     * @throws IOException
     */
    public static String sendGetData(String url) throws ClientProtocolException, IOException {
        String result = "";

        // 创建httpclient对象
        CloseableHttpClient httpClient = HttpClients.createDefault();

        // 创建get方式请求对象
        HttpGet httpGet = new HttpGet(url);
        httpGet.addHeader("Content-type", "application/json");
        // 通过请求对象获取响应对象
        CloseableHttpResponse response = httpClient.execute(httpGet);

        // 获取结果实体
        // 判断网络连接状态码是否正常(0--200都是正常)
        if (response.getStatusLine().getStatusCode() == HttpStatus.SC_OK) {
            result = EntityUtils.toString(response.getEntity(), "utf-8");
        }
        // 释放链接
        response.close();

        return result;
    }

}

五、封装Json数据转换工具类

package com.chen.mykinthtest.util;

import com.alibaba.fastjson.JSON;

import java.util.Map;

/**
 * Json数据转换工具类
 *
 * @author chen
 */
public class JsonUtil {

    /**
     * 将Json字符串转成Map
     *
     * @param jsonString
     * @return map
     */
    public static Map parseJsonToMap(String jsonString) {
        Map map = JSON.parseObject(jsonString, Map.class);
        System.err.println("Json转Map:");
        for (Object obj : map.keySet()) {
            System.err.print(obj + "-" + map.get(obj));
        }
        System.err.println();
        return map;
    }

    /**
     * 将Map转换成Json
     *
     * @param map
     * @return
     */
    public static String parseMapToJson(Map<String, Object> map) {
        String json = JSON.toJSONString(map);
        System.err.println("Map转Json:");
        System.err.println(json);
        return json;
    }

}

六、编写控制器进行调用第三方接口

注:我将第三方接口也写到了同一个类中(因为懒),但实现效果是一样的。

package com.chen.mykinthtest.controller;

import com.chen.mykinthtest.restful.RestResponse;
import com.chen.mykinthtest.service.HttpUtil;
import com.chen.mykinthtest.util.JsonUtil;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Http通信控制器
 *
 * @author chen
 */
@RestController
public class HttpController {

    private String server_path = "http://localhost:8080";

    /**
     * 模拟第三方接口,Post的方式接收普通参数
     *
     * @param id
     * @return
     */
    @RequestMapping("helloMap")
    public RestResponse helloMap(int id) {
        if (id == 1) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", 1);
            map.put("name", "cws");
            map.put("sex", "M");
            return RestResponse.ok().msg("处理成功").item(map);
        } else {
            return RestResponse.fail().msg("处理失败");
        }
    }

    /**
     * 模拟第三方接口,Post的方式接收Json数据
     *
     * @param jsonMap
     * @return
     */
    @RequestMapping("helloJson")
    public RestResponse helloJson(@RequestBody Map jsonMap) {
        int id = (int) jsonMap.get("id");
        if (id == 2) {
            Map<String, Object> map = new HashMap<String, Object>();
            map.put("id", 2);
            map.put("name", "lhs");
            map.put("sex", "F");
            return RestResponse.ok().msg("处理成功").item(map);
        } else {
            return RestResponse.fail().msg("处理失败");
        }
    }

    /**
     * 模拟第三方接口,Get的方式
     *
     * @return
     */
    @RequestMapping("helloGet")
    public RestResponse helloGet() {
        return RestResponse.ok().msg("处理成功");
    }

    /**
     * 以map的形式发送请求数据
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("testSendPostDataByMap")
    public String testSendPostDataByMap() throws IOException {
        String api_path_map = "/helloMap";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 1);
        String body = HttpUtil.sendPostDataByMap(server_path + api_path_map, map);
        System.err.println("发送Map数据的响应结果:" + body);
        Map<String, Object> bodyMap = JsonUtil.parseJsonToMap(body);
        System.err.println("获得返回的状态码:error-----" + bodyMap.get("error"));
        Map<String, Object> itemMap = (Map<String, Object>) bodyMap.get("item");
        for (Map.Entry<String, Object> entry : itemMap.entrySet()) {
            System.err.println(entry.getKey() + "----" + entry.getValue());
        }
        return body;
    }

    /**
     * 以Json的形式发送请求数据
     *
     * @return
     * @throws IOException
     */
    @RequestMapping("testSendPostDataByJson")
    public String testSendPostDataByJson() throws IOException {
        String api_path_json = "/helloJson";
        Map<String, Object> map = new HashMap<String, Object>();
        map.put("id", 2);
        String json = JsonUtil.parseMapToJson(map);
        String body = HttpUtil.sendPostDataByJson(server_path + api_path_json, json);
        System.err.println("发送Json数据的响应结果:" + body);
        Map<String, Object> bodyMap = JsonUtil.parseJsonToMap(body);
        System.err.println("获得返回的状态码:error-----" + bodyMap.get("error"));
        Map<String, Object> itemMap = (Map<String, Object>) bodyMap.get("item");
        for (Map.Entry<String, Object> entry : itemMap.entrySet()) {
            System.err.println(entry.getKey() + "----" + entry.getValue());
        }
        return body;
    }

    /**
     * get方式发送请求,至于请求参数可以自己手动加到访问地址后面
     * @return
     * @throws IOException
     */
    @RequestMapping("testSendGetData")
    public String testSendGetData() throws IOException {
        String api_path_get = "/helloGet";
        String body = HttpUtil.sendGetData(server_path + api_path_get);
        return body;
    }
}

七、测试结果

用map数据发送请求:

 

以json数据发送请求:

用get发送请求:

发布了125 篇原创文章 · 获赞 67 · 访问量 6万+

猜你喜欢

转载自blog.csdn.net/qq_42109746/article/details/104033421