Hutool工具裏,POST方法,body中傳參的幾種調用方法

接口說明:java

POSTMAN測試:json

JAVA代碼:api

package com.provy.guard.api;

import java.util.HashMap;
import java.util.Map;

import cn.hutool.http.HttpUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;

/**
 * Hutool工具裏,POST方法,body中傳參的幾種調用方法
 * @author wangxy
 *
 */
public class Test {

    public static void main(String[] args) {
        post1();// 成功
        post2();// 成功
        post3();// 失敗
        post4();// 成功
    }

    public static void post1() {
        String param = "{\"StartTime\":\"2018-10-10 09:42:02\",\"EndTime\":\"2018-10-29 09:42:02\",\"StartIndex\":\"0\",\"PageSize\":\"30\"}";
        String result = HttpUtil.post("http://localhost:8080/v1/hitrecord/2", param);
        System.out.println(result);
    }

    public static void post2() {
        JSONObject param = JSONUtil.createObj();
        param.put("StartTime", "2018-10-10 09:42:02");
        param.put("EndTime", "2018-10-29 09:42:02");
        param.put("StartIndex", 0);
        param.put("PageSize", 30);
        String result = HttpUtil.post("http://localhost:8080/v1/hitrecord/2", param.toString());
        System.out.println(result);
    }

    public static void post3() {
        Map<String, Object> param = new HashMap<String, Object>();
        param.put("StartTime", "2018-10-10 09:42:02");
        param.put("EndTime", "2018-10-29 09:42:02");
        param.put("StartIndex", "0");
        param.put("PageSize", "30");
        String result = HttpUtil.post("http://localhost:8080/v1/hitrecord/2", param);
        System.out.println(result);
    }

    public static void post4() {
        JSONObject json = JSONUtil.createObj();
        String result = HttpUtil.createPost("http://localhost:8080/v1/hitrecord/2").body(json).execute().body();
        System.out.println(result);
    }

}
相關文章
相關標籤/搜索