当前位置:   article > 正文

万字长文讲解调用第三方接口,RestTemplate,urlConnection使用详解,java代码模拟postman发送请求_java模拟postman发送请求

java模拟postman发送请求

业务场景:在日常开发中,经常需要调用第三方接口,例如调用物流接口,此时需要利用urlConnection或者restTemplate模拟postman发送请求,请求支持加header ,设置content-type支持传递json;请求方式get,post,也可以需要传递文件,或者传递文件流;

下面这个例子就包含日常开发中大部分的请求,可以直接运行,觉得有用收藏点赞不迷路。

新建springmvc项目,项目结构图

image-20230616103929090

pom文件

<dependencies>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-web</artifactId>
    </dependency>

    <dependency>
        <groupId>org.projectlombok</groupId>
        <artifactId>lombok</artifactId>
        <optional>true</optional>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>

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

    <dependency>
        <groupId>com.alibaba</groupId>
        <artifactId>fastjson</artifactId>
        <version>1.2.68</version>
    </dependency>
</dependencies>
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34

配置文件

server.port=8089
spring.servlet.multipart.max-file-size = 100MB
spring.servlet.multipart.max-request-size = 1000MB
  • 1
  • 2
  • 3

User类

import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class User {
    String name;
    int age;

    public User(String name) {
        this.name = name;
    }

    @Override
    public String toString() {
        return "User{" +
                "name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23

UserController类

import com.alibaba.fastjson.JSON;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.util.LinkedHashMap;
import java.util.Map;
@RestController
@RequestMapping("/user")
public class UserController {

        @GetMapping("/loginGet")
        public String loginGet(String name,Integer age){
            System.out.println(name+""+age);
            User user=new User(name,age);
            return JSON.toJSONString(user);
        }

        @PostMapping("/login")
        public String login(User user){
            System.out.println(user);
            return JSON.toJSONString(user);
        }

        @PostMapping("/upload")
        public Map<String, String> login(User user, @RequestParam("file") MultipartFile file){
            System.out.println(user);
            System.out.println(file.getName());
            Map<String, String> fileMetadata = new LinkedHashMap<>();
            fileMetadata.put("文件名", file.getOriginalFilename());
            fileMetadata.put("文件类型", file.getContentType());
            fileMetadata.put("文件大小(byte)", String.valueOf(file.getSize()));
            return fileMetadata;
        }


        @PostMapping("/loginJson")
        public String loginJson(@RequestBody User user){
            System.out.println(user);
            return JSON.toJSONString(user);
        }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41

post方式 x-www-form-urlencoded key-value只能是string

image-20230616105720200

对应java代码

import lombok.extern.slf4j.Slf4j;
import org.apache.http.HttpEntity;
import org.apache.http.NameValuePair;
import org.apache.http.client.entity.UrlEncodedFormEntity;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
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.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * @ClassName JavaNice
 * post x-www-form-urlencoded  key-value只能是string
 **/
@Slf4j
public class PostUtils {
    private static final String ENCODING = "UTF-8";

    /**
     * post x-www-form-urlencoded  key-value只能是string*
     * @param url
     * @param paramsMap
     * @return
     */
    public static String sendPost(String url, Map<String, String> paramsMap) {
        CloseableHttpClient client = HttpClients.createDefault();
        String responseText = "";
        CloseableHttpResponse response = null;
        try {
            HttpPost method = new HttpPost(url);
            if (paramsMap != null) {
                List<NameValuePair> paramList = new ArrayList<NameValuePair>();
                for (Map.Entry<String, String> param : paramsMap.entrySet()) {
                    NameValuePair pair = new BasicNameValuePair(param.getKey(), param.getValue());
                    paramList.add(pair);
                }
                method.setEntity(new UrlEncodedFormEntity(paramList, ENCODING));
            }
            response = client.execute(method);
            HttpEntity entity = response.getEntity();
            if (entity != null) {
                responseText = EntityUtils.toString(entity);
            }
        } catch (Exception e) {
            log.error("http request failed",e);
        } finally {
            try {
                response.close();
            } catch (Exception e) {
                log.error("",e);
            }
        }
        return responseText;
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60

调用代码

@Test
void postString() {
    //post x-www-form-urlencoded  key-value只能是string
    Map<String,String> user=new HashMap<>();
    user.put("name","javaNice");
    String json = JSONObject.toJSONString(user);
    Map<String,String> paramsMap = JSONObject.parseObject(json,Map.class);
    System.out.println(json);
    String resp = PostUtils.sendPost("http://localhost:8089/user/login",paramsMap);
    System.out.println(resp);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11

get 方式 传递参数拼接在url上

image-20230616110813390

post 方式 传递参数url拼接

image-20230616111016573

post 方式 传递参数为json

image-20230616111100395

工具类

import com.alibaba.fastjson.JSONObject;
import org.springframework.util.StringUtils;

import java.io.*;
import java.net.HttpURLConnection;
import java.net.URL;
import java.net.URLConnection;
import java.net.URLEncoder;
import java.util.List;
import java.util.Map;

public class HttpRequestUtil {

    public static String sendGet(String url, String param) {
        String result = "";
        BufferedReader in = null;
        try {
            if (!url.contains("?")) {
                url += "?1=1";
            }
            if (!param.startsWith("&")) {
                url += "&";
            }
            String urlNameString = url + param;
            URL realUrl = new URL(urlNameString);
            // 打开和URL之间的连接
            URLConnection connection = realUrl.openConnection();
            // 设置通用的请求属性
            connection.setRequestProperty("accept", "*/*");
            connection.setRequestProperty("connection", "Keep-Alive");
            connection.setRequestProperty("user-agent",
                    "Mozilla/4.0 (compatible; MSIE 6.0; Windows NT 5.1;SV1)");
            // 建立实际的连接
            connection.connect();
            // 获取所有响应头字段
            Map<String, List<String>> map = connection.getHeaderFields();
            // 遍历所有的响应头字段
//            for (String key : map.keySet()) {
//                System.out.println(key + "--->" + map.get(key));
//            }
            // 定义 BufferedReader输入流来读取URL的响应
            in = new BufferedReader(new InputStreamReader(
                    connection.getInputStream()));
            String line;
            while ((line = in.readLine()) != null) {
                result += line;
            }
        } catch (Exception e) {
            System.out.println("发送GET请求出现异常!" + e);
            e.printStackTrace();
        }
        // 使用finally块来关闭输入流
        finally {
            try {
                if (in != null) {
                    in.close();
                }
            } catch (Exception e2) {
                e2.printStackTrace();
            }
        }
        return result;
    }


    public static String sendPost(String url, String param) {
        HttpURLConnection httpURLConnection = null;
        OutputStream out = null; //写
        InputStream in = null;   //读
        int responseCode = 0;    //远程主机响应的HTTP状态码
        String result = "";
        try {
            URL sendUrl = new URL(url);
            httpURLConnection = (HttpURLConnection) sendUrl.openConnection();
            //post方式请求
            httpURLConnection.setRequestMethod("POST");
            //一定要设置 Content-Type 要不然服务端接收不到参数
            httpURLConnection.setRequestProperty("Content-Type", "application/json");
            //指示应用程序要将数据写入URL连接,其值默认为false(是否传参)
            httpURLConnection.setDoOutput(true);
            //httpURLConnection.setDoInput(true);
            httpURLConnection.setUseCaches(false);
            httpURLConnection.setConnectTimeout(30000); //30秒连接超时
            httpURLConnection.setReadTimeout(30000);    //30秒读取超时
            //获取输出流
            out = httpURLConnection.getOutputStream();
            //输出流里写入POST参数
            out.write(param.getBytes());
            out.flush();
            out.close();
            responseCode = httpURLConnection.getResponseCode();
            BufferedReader br = new BufferedReader(new InputStreamReader(httpURLConnection.getInputStream(), "UTF-8"));
            result = br.readLine();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return result;
    }

    public static String objectToUrlParam(Object object) {
        if (object != null) {
            StringBuilder builder = new StringBuilder();
//            JSONObject jsonObject = JSON.parseObject(JSON.toJSONString(object));
            Map<String, Object> map = JSONObject.parseObject(JSONObject.toJSONString(object), Map.class);
            for (Map.Entry<String, Object> entry :
                    map.entrySet()) {
                if (entry.getValue() != null && !StringUtils.isEmpty(entry.getValue().toString())) {
                    builder.append("&").append(entry.getKey()).append("=").append(encode(entry.getValue()));
                }
            }
            return builder.toString();
        }
        return "";
    }

    public static String encode(Object object) {
        if (object != null) {
            try {
                return URLEncoder.encode(object.toString(), "utf-8");
            } catch (UnsupportedEncodingException e) {
                e.printStackTrace();
                throw new RuntimeException("字符串编码失败");
            }
        }
        return "";
    }

}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128

调用代码

@Test
void get() {
    //get 方式 传递参数拼接在url上
    User user=new User("javaNice",666);
    String paramsMap = HttpRequestUtil.objectToUrlParam(user);
    String resp = HttpRequestUtil.sendGet("http://localhost:8089/user/loginGet",paramsMap);
    System.out.println(resp);
}


@Test
void postUrl() {
    //post 方式 传递参数url拼接
    User user=new User("javaNice",666);
    String paramsMap = HttpRequestUtil.objectToUrlParam(user);
    System.out.println("传递参数"+paramsMap);
    String url="http://localhost:8089/user/login?1=1"+paramsMap;
    String resp = HttpRequestUtil.sendPost(url,"");
    System.out.println("返回参数"+resp);
}

@Test
void post() {
    //post 方式 传递参数为json
    User user=new User("javaNice",666);
    String paramsMap = JSON.toJSONString(user);
    System.out.println("传递参数"+paramsMap);
    String resp = HttpRequestUtil.sendPost("http://localhost:8089/user/loginJson",paramsMap);
    System.out.println("返回参数"+resp);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30

RestTemplate方式

post 方式 传递参数为json

image-20230616111504260

@Test
void post() {
    //post 方式 传递参数为json
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8089/user/loginJson";
    User body = new User("javaNice", 666);
    User result = restTemplate.postForObject(url, body, User.class);
    System.out.println("返回参数" + result);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9

post 方式 传递参数为form-data 并且传递文件并添加请求头

image-20230616111929793

image-20230616112027790

@Test
void postFormFile() {
    //post 方式 传递参数为form-data 并且传递文件
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8089/user/upload";
    //①:表单信息,需要放在MultiValueMap中,MultiValueMap相当于Map<String,List<String>>
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    //调用add方法放入表单元素(表单名称:值)
    //②:文件对应的类型,需要是org.springframework.core.io.Resource类型的,常见的有[FileSystemResource、InputStreamResource、ByteArrayResource]
    body.add("file", new FileSystemResource("/java/com/example/springmvc/22c017e48ea445199708dc1d2832dae5.pdf"));
    body.add("name", "javaNice");
    body.add("age", "666");
    //③:头
    HttpHeaders headers = new HttpHeaders();
    headers.add("header1", "v1");
    headers.add("header2", "v2");
    //④:请求实体
    RequestEntity<MultiValueMap<String, Object>> requestEntity = new RequestEntity<>(body, headers, HttpMethod.POST, URI.create(url));
    //⑤:发送请求(请求实体,返回值需要转换的类型)
    ResponseEntity<Map<String, String>> responseEntity = restTemplate.exchange(
            requestEntity,
            new ParameterizedTypeReference<Map<String, String>>() {
            });
    Map<String, String> result = responseEntity.getBody();
    System.out.println(result);
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26

post 通过流的方式上传文件

image-20230616111929793

image-20230616112027790

@Test
void postFormFileStream() {
    RestTemplate restTemplate = new RestTemplate();
    String url = "http://localhost:8089/user/upload";
    //①:表单信息,需要放在MultiValueMap中,MultiValueMap相当于Map<String,List<String>>
    MultiValueMap<String, Object> body = new LinkedMultiValueMap<>();
    /**
     * ②:通过流的方式上传文件,流的方式需要用到InputStreamResource类,需要重写2个方法
     * getFilename:文件名称
     * contentLength:长度
     */
    File file = new File("/java/com/example/springmvc/22c017e48ea445199708dc1d2832dae5.pdf");
    //2、建立数据通道
    try {
        InputStream inputStream = new FileInputStream(file);

        InputStreamResource inputStreamResource = new InputStreamResource(inputStream) {
            @Override
            public String getFilename() {
                return "22c017e48ea445199708dc1d2832dae5.pdf";
            }

            @Override
            public long contentLength() throws IOException {
                return inputStream.available();
            }
        };
        body.add("file", inputStreamResource);
        body.add("name", "javaNice");
        body.add("age", "666");
        //③:头
        HttpHeaders headers = new HttpHeaders();
        headers.add("header1", "v1");
        headers.add("header2", "v2");
        //④:请求实体
        RequestEntity<MultiValueMap<String, Object>> requestEntity = new RequestEntity<>(body, headers, HttpMethod.POST, URI.create(url));
        //⑤:发送请求(请求实体,返回值需要转换的类型)
        ResponseEntity<Map<String, String>> responseEntity = restTemplate.exchange(
                requestEntity,
                new ParameterizedTypeReference<Map<String, String>>() {
                });
        Map<String, String> result = responseEntity.getBody();
        System.out.println(result);
    } catch (FileNotFoundException e) {
        e.printStackTrace();
    }
}
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/不正经/article/detail/496681
推荐阅读
相关标签
  

闽ICP备14008679号