对接第三方接口

该博客介绍了如何在Java中利用Hutool库进行HTTP请求,包括GET和POST方法,涉及设置请求参数、请求头以及处理响应。通过引入Apache HttpClient和Fastjson依赖,实现了对JSON和Map类型数据的处理。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

1.导入依赖

        <dependency>
            <groupId>org.apache.httpcomponents</groupId>
            <artifactId>httpclient</artifactId>
            <version>4.5.13</version>
        </dependency>

        <!-- JSON 解析器和生成器 -->
        <dependency>
            <groupId>com.alibaba</groupId>
            <artifactId>fastjson</artifactId>
            <version>1.2.75</version>
        </dependency>
        <!-- hutool -->
        <dependency>
            <groupId>cn.hutool</groupId>
            <artifactId>hutool-all</artifactId>
            <version>4.5.11</version>
        </dependency>

2.工具类HuHttpUtil

import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpUtil;



public class HuHttpUtil {

    public static String get(HttpDto request) {

        String response = null;
        if (request.getUrl()!=null) {

            return null;
        }
        try {
            HttpRequest get = HttpUtil.createGet(request.getUrl());
            if (request.getMapRequest() != null && request.getMapRequest().size() > 0) {
                get.form(request.getMapRequest());
            }
            if (request.getJsonRequest()!=null) {
                get.form(request.getJsonRequest());
            }
            if (request.getToken()!=null) {
                get.header("token", request.getToken()); //这个请求头.header是自己项目需要加的,可以省略
            }
            response = get.execute()
                    .charset("utf-8")  //返回参数格式utf-8
                    .body();
        } catch (Exception e) {

        }
        return response;
    }

    public static String post(HttpDto request) {

        String response = null;
        if (request.getUrl()!=null) {

            return null;
        }
        try {
            HttpRequest post = HttpUtil.createPost(request.getUrl());

            if (request.getMapRequest() != null && request.getMapRequest().size() > 0) {
                post.form(request.getMapRequest());
            }
            if (request.getJsonRequest()!=null) {
                post.body(request.getJsonRequest());

            }
            if (request.getToken()!=null) {
                post.header("token", request.getToken()); //这个请求头.header是自己项目需要加的,可以省略
            }

            if(request.getContentType()!=null){
                post.header("Content-Type", "application/json"); //这个请求头.header
            }else {
                post.header("Content-Type", request.getContentType()); //这个请求头.header
            }
            response = post.execute()
                    .charset("utf-8")  //返回参数格式utf-8
                    .body();
        } catch (Exception e) {

        }
        return response;
    }

}

3.工具类HttpDto

import java.util.Map;

/**
 * hutool 的 http请求类
 */
public class HttpDto {

    public final static String HTTP_GET = "GET";
    public final static String HTTP_POST = "POST";

    private String type;
    /**
     * 请求url
     */
    private String url;
    /**
     * map参数
     */
    private Map<String,Object> mapRequest;
    /**
     * json参数
     */
    private String jsonRequest;
    /**
     * token
     */
    private String token;

    private String contentType;

    public String getUrl() {
        return url;
    }

    public void setUrl(String url) {
        this.url = url;
    }

    public Map<String, Object> getMapRequest() {
        return mapRequest;
    }

    public void setMapRequest(Map<String, Object> mapRequest) {
        this.mapRequest = mapRequest;
    }

    public String getJsonRequest() {
        return jsonRequest;
    }

    public void setJsonRequest(String jsonRequest) {
        this.jsonRequest = jsonRequest;
    }

    public String getToken() {
        return token;
    }

    public void setToken(String token) {
        this.token = token;
    }

    public String getType() {
        return type;
    }

    public void setType(String type) {
        this.type = type;
    }

    public String getContentType() {
        return contentType;
    }

    public void setContentType(String contentType) {
        this.contentType = contentType;
    }
}

4.请求方法

    public static String getResponse(String url, String jsonRequest, Map<String, Object> mapRequest, String httpType) {

        HttpDto request = new HttpDto();
        request.setUrl(dataUrl);
        if (StringUtils.isBlank(jsonRequest)) {
            request.setJsonRequest(jsonRequest);
        }
        if (mapRequest != null) {
            request.setMapRequest(mapRequest);
        }
        request.setType(httpType);
        request.setContentType("application/x-www-form-urlencoded");
        String response = null;
        try {
        if(httpType.equals("POST")){
           response = HuHttpUtil.post(request);
        }else{
           response = HuHttpUtil.get(request);
        }
         
        } catch (Exception e) {
            logger.info("调用失败" + e.getMessage());
        }
        System.out.println("response----" + response);
        return response;
    }

6.调用

 JSONObject param = new JSONObject();
        param.put("interfaceName", "getRecordContent");
        JSONObject requestParam = new JSONObject();
        requestParam.put("name", "小小");     
        requestParam.put("age", 18);     
        param.put("requestParam", requestParam.toJSONString());
       // param.put("token", token);   //如果需要token

        Map<String, Object> paramFinal = new HashMap<>();
        paramFinal.put("Param", JSON.toJSONString(param));

        String response = null;
        try {
            response = getResponse(dataUrl, null, paramFinal, HttpDto.HTTP_POST);
//只要对response做处理就可以
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值