有时候我们调试外部接口的时候想拿到数据入库或者是显示的话,首先都是需要使用postMan调试通过在进行下一步的,这时候我们可以直接使用postMan导出java代码复制粘贴就ok了。
1, 打开postman运行接口
2,点击右上角的code
3,依次点击HTTP,java,OkHttp
4,最后生成java代码复制到代码中
个人案例参考:
OkHttpClient client = new OkHttpClient().newBuilder().build();
Request request = new Request.Builder()
.url("http://10.227.112.112:8088/api/v1/warning")
.method("GET", null)
.build();
String str = "";
try {
Response response = client.newCall(request).execute();
ResponseBody body = response.body();
str = body.string();
}catch (Exception e){
log.info("response请求返回数据:" + e.getMessage());
}
System.out.print("接口返回数据:"+str);
Maven依赖
<!-- https://mvnrepository.com/artifact/com.squareup.okhttp3/okhttp -->
<dependency>
<groupId>com.squareup.okhttp3</groupId>
<artifactId>okhttp</artifactId>
<version>4.9.1</version>
</dependency>