栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

Java内部利用RestTemplate发送Http请求

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

Java内部利用RestTemplate发送Http请求

1.新建请求工厂:

RestTemplate 默认有两种工厂对象实现方式,都是 ClientHttpRequestFactory 的子类。如下:

1)SimpleClientHttpRequestFactory 底层使用 java.net.HttpUrlConnection,可配置证书。

2)HttpComponentsClientHttpRequestFactory 底层使用Apache HttpClient访问远程的Http服务,使用HttpClient同样可以配置连接池和证书等信息,而且功能更强大,配置项更多。

这里使用的是SimpleClientHttpRequestFactory工厂对象:

//创建SpringHttp请求工厂对象
SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
//设置连接超时时间,默认 -1
requestFactory.setConnectTimeout(10*1000);
//设置读取内容超时时间,默认 -1
requestFactory.setReadTimeout(10*1000);
//设置是否使用缓冲流来存储请求体,默认是 true,缺点是当发送大量数据时,比如put/post的保存和修改,那么可能内存消耗严重。
requestFactory.setBufferRequestBody(false);
 2.创建Spring提供的Http请求客户端RestTemplate对象用于发送请求:
//创建请求客户端,并将SpringHttp请求工厂requestFactory传入构造方法
RestTemplate client = new RestTemplate(requestFactory);
 3.创建HTTP请求头:

public void setContentType(@Nullable MediaType mediaType)  设置请求正文(body)的媒体类型,MediaType决定浏览器将以什么形式、什么编码对资源进行解析

HttpHeaders headers = new HttpHeaders();
//设置请求正文(body)的媒体类型,MediaType决定浏览器将以什么形式、什么编码对资源进行解析
headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8);
 4.创建HTTP请求实体HttpEntity:
//HttpEntity表示一个 HTTP 请求或响应实体,由头部和主体组成: new HttpEntity( requestBody, MultiValueMap headers);
org.springframework.http.HttpEntity requestEntity = new org.springframework.http.HttpEntity(JSONObject.toJSON("{a:1,b:2}"), headers);
5.用RestTemplate的exchange()方法发送请求, 并用 ResponseEntity 接收响应主体(ResponseBody)的内容:
//RestTemplate.exchange(String URL(请求地址), HttpMethod method(请求方法), HttpEntity requestEntity(请求主体), Class responseType(返回类型), [Object... uriVariables])
ResponseEntity httpres = client.exchange(URL.toString(), HttpMethod.POST, requestEntity, String.class);
完整请求过程: 
import org.springframework.http.*;
import org.springframework.http.client.SimpleClientHttpRequestFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import java.lang.Exception;
import java.util.*;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;

@Controller
@RestController
@RequestMapping("/api/AFS_ORDER")
public class AFS_OrderController extends baseController {

@GetMapping("/sendInfo")
@ResponseBody
public Object sendInfo(@RequestParam(value="isBatch",required = false,defaultValue = "否")String isBatch,@RequestParam(value="objectId",required = false)String objectId) throws Exception {
try {        
    SimpleClientHttpRequestFactory requestFactory = new SimpleClientHttpRequestFactory();
    //设置连接超时时间,默认 -1
    requestFactory.setConnectTimeout(10*1000);
    //设置读取内容超时时间,默认 -1
    requestFactory.setReadTimeout(10*1000);
    //设置是否使用缓冲流来存储请求体,默认是 true,缺点是当发送大量数据时,比如put/post的保存和修改,那么可能内存消耗严重。
    requestFactory.setBufferRequestBody(false);

    RestTemplate client = new RestTemplate(requestFactory);

    HttpHeaders headers = new HttpHeaders();
    //设置请求正文(body)的媒体类型,MediaType决定浏览器将以什么形式、什么编码对资源进行解析
    headers.setContentType(MediaType.APPLICATION_PROBLEM_JSON_UTF8);

    //HttpEntity表示一个 HTTP 请求或响应实体,由头部和主体组成: new HttpEntity( requestBody, MultiValueMap headers);
    HttpEntity requestEntity = new HttpEntity(JSONObject.toJSON("{a:1,b:2}"), headers);

    //RestTemplate.exchange(String URL(请求地址), HttpMethod method(请求方法), HttpEntity requestEntity(请求主体), Class responseType(返回类型), [Object... uriVariables])
    ResponseEntity httpres = client.exchange(URL.toString(), HttpMethod.POST, requestEntity, String.class);
    }catch(Exception e){
        throw new RuntimeException(e);
    }
}

}

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/605621.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号