hutool put and delete requests

hutool put and delete requests

Hutool is a powerful tool class. Today we will record the way of sending RestFul style requests in the tool class.

  • Import the jar package before use, you can go to MavenRepository to search for hutool, select Hutool All to import pom, or you can download the jar package to use
<!-- https://mvnrepository.com/artifact/cn.hutool/hutool-all -->
<dependency>
    <groupId>cn.hutool</groupId>
    <artifactId>hutool-all</artifactId>
    <version>5.5.8</version>
</dependency>
  • Post and Get requests
    These two requests are very simple. The tool class has its own packaging tool HttpUtil. This tool class request returns a string of JSON objects. You can use the tool class's built-in JSONUtil to parse it. example:

//所有的工具类都是hutool本身自带的
//POST请求  requestUrl是请求路径,paramMap是Map参数,参数还可以是字符串body
String resultJson1 = HttpUtil.post(requestUrl, paramMap);
JSONObject jsonObject1 = JSONUtil.parseObj(resultJson);
//GET请求 也可带Map集合参数
String resultJson2 = HttpUtil.get(requestUrl);
JSONObject jsonObject2 = JSONUtil.parseObj(resultJson);
  • Put and Delete requests
    do not have Put and Delete request methods in HttpUtil. Click the package method in the tool class
    Insert picture description here
    . As can be seen from the above figure, the request is made using HttpRequest. And HttpRequest encapsulates Get, Delete, Post, Put, and Option requests. So you can follow the above request method; for
    example:
//DELETE请求,requestUrl请求路径,paramMap请求参数
String resultJson3 = HttpRequest.delete(requestUrl).form(paramMap).execute().body();
JSONObject jsonObject3 = JSONUtil.parseObj(resultJson);
//PUT请求,requestUrl请求路径,paramMap请求参数
String resultJson4 =  HttpRequest.put(requestUrl).form(paramMap).execute().body()
JSONObject jsonObject4 = JSONUtil.parseObj(resultJson);

Guess you like

Origin blog.csdn.net/qq_30385099/article/details/113718766