文章目录
- 前言
- 依赖环境
- Hutool 请求验证
- 准备第三方接口
- 编写接口调用上述定义接口
- 自测验证
- get 请求
- post 非常规 application/x-www-form-urlencoded
- post 常规 application/json
前言
在实际开发过程中,微服务环境下往往采取openfeign
实现服务与服务之间的请求调用。但有时候需要调用第三方API
的情况,虽然在spring boot 框架中提供了RestTemplate
请求模板,但这个不怎么好用。
市面上支持http调用的框架技术很多,比如okhttp
等。本篇文章重点说明Hutool
给我们封装的请求方法类。
依赖环境
使用Hutool
,需要引入对应的依赖。如下:
<dependency><groupId>cn.hutool</groupId><artifactId>hutool-all</artifactId><version>5.7.5</version>
</dependency>
Hutool 请求验证
准备第三方接口
准备get
、常规 post
、非常规post
两种接口。
import cn.xj.model.Result;
import cn.xj.model.UserVo;
import org.springframework.web.bind.annotation.*;import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/test")
public class TestController {/*** 常规 get 请求* @param name* @param age* @return*/@GetMapping("/get")public Result get(String name,Integer age){Map<String,Object> map = new HashMap<>();map.put("name",name);map.put("age",age);return Result.success(map);}/*** post 请求,未指定 Content-Type 时,默认是 application/json,参数传递在boby* 若指定类型为 application/x-www-form-urlencoded,参数传递在请求头上,类似get* @param name* @param age* @return*/@PostMapping("/post1")public Result post1(@RequestParam String name,@RequestParam Integer age){Map<String,Object> map = new HashMap<>();map.put("name",name);map.put("age",age);return Result.success(map);}@PostMapping("/post2")public Result post2(@RequestBody UserVo body){Map<String,Object> map = new HashMap<>();map.put("name2",body.getName());map.put("age2",body.getAge());map.put("body",body);return Result.success(map);}
}
【注意】这里做一点说明。
post 请求,
未指定 Content-Type 时,默认是 application/json,参数传递在boby
若指定类型为 application/x-www-form-urlencoded,参数传递在请求头上,类似get
参考资料:
Axios请求头中常见的Content-Type常见的三种格式
编写接口调用上述定义接口
import cn.hutool.http.ContentType;
import cn.hutool.http.Header;
import cn.hutool.http.HttpRequest;
import cn.hutool.http.HttpResponse;
import cn.xj.model.Result;
import com.alibaba.fastjson.JSONObject;
import org.springframework.web.bind.annotation.*;
import java.util.HashMap;
import java.util.Map;@RestController
@RequestMapping("/http")
public class HttpTestController {/*** 调用post 接口发送get请求* @return*/@PostMapping("/post/get")public Result getRequest(){// 设定headermap get 请求对请求头的设置没影响HashMap<String, String> headerMap = new HashMap<>();headerMap.put(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue()); // application/json//headerMap.put(Header.CONTENT_TYPE.getValue(), ContentType.FORM_URLENCODED.getValue()); // application/x-www-form-urlencodedHashMap<String, Object> bodyMap = new HashMap<>();bodyMap.put("name","香蕉");bodyMap.put("age","20");// 请求HttpResponse response = HttpRequest.get("http://localhost:80/test/get").form(bodyMap) // url 上加参数.addHeaders(headerMap).timeout(20000).execute();return Result.success(response.body());}/*** 调用post 查询第三方post请求* @param type 类别* 0、application/x-www-form-urlencoded* 1、application/json* @return*/@PostMapping("/post/post")public Result postRequest(@RequestParam Integer type){HashMap<String, Object> bodyMap = new HashMap<>();bodyMap.put("name","香蕉");bodyMap.put("age","20");String bodyStr = "";String url = "";HashMap<String, String> headerMap = new HashMap<>();if(0 == type){headerMap.put(Header.CONTENT_TYPE.getValue(), ContentType.FORM_URLENCODED.getValue()); // application/x-www-form-urlencoded// post 请求默认是 application/json,如果是 application/x-www-form-urlencoded 则需要保证参数在url后StringBuilder sb = new StringBuilder();for (Map.Entry<String, Object> stringObjectEntry : bodyMap.entrySet()) {String key = stringObjectEntry.getKey();Object value = stringObjectEntry.getValue();sb.append(key).append("=").append(value).append("&");}bodyStr = sb.deleteCharAt(sb.length() - 1).toString();url = "http://localhost:80/test/post1";}else{headerMap.put(Header.CONTENT_TYPE.getValue(), ContentType.JSON.getValue()); // application/jsonbodyStr = JSONObject.toJSONString(bodyMap);url = "http://localhost:80/test/post2";}// 请求HttpResponse response = HttpRequest.post(url)// .form(bodyMap) // url 上加参数.body(bodyStr).addHeaders(headerMap).timeout(20000).execute();return Result.success(response.body());}
}
自测验证
get 请求
使用get
请求时,不管设定的Content-Type
是application/json
,亦或者application/x-www-form-urlencoded
,传递数据在body时,都不会被解析。
http://localhost/http/post/get
post 非常规 application/x-www-form-urlencoded
http://localhost/http/post/post?type=0
请求后的数据返回样式如下:
post 常规 application/json
http://localhost/http/post/post?type=2
请求后的数据返回样式如下: