当前位置: 首页 > news >正文

168推广东莞seo快速排名

168推广,东莞seo快速排名,彩票软件开发定制费用,华竣国际的展厅设计公司简介 RestTemplate是一个执行HTTP请求的同步阻塞式工具类,它仅仅只是在 HTTP 客户端库(例如 JDK HttpURLConnection,Apache HttpComponents,okHttp 等)基础上,封装了更加简单易用的模板方法 API&#xff0c…

简介

RestTemplate是一个执行HTTP请求的同步阻塞式工具类,它仅仅只是在 HTTP 客户端库(例如 JDK HttpURLConnection,Apache HttpComponents,okHttp 等)基础上,封装了更加简单易用的模板方法 API,方便程序员利用已提供的模板方法发起网络请求和处理,能很大程度上提升我们的开发效率

依赖

1、非Spring环境下使用RestTemplate

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

2、Spring环境下使用 RestTemplate

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

初始化配置

@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(){RestTemplate restTemplate = new RestTemplate(simpleClientHttpRequestFactory());return restTemplate;}public ClientHttpRequestFactory simpleClientHttpRequestFactory() {SimpleClientHttpRequestFactory factory = new SimpleClientHttpRequestFactory();factory.setReadTimeout(150000); // msfactory.setConnectTimeout(150000); // msreturn factory;}}

这种初始化方法,是使用了JDK自带的HttpURLConnection作为底层HTTP客户端实现。

可以执行配置不同的连接方式,如

HttpClient

@Configuration
public class RestTemplateConfig {@Beanpublic RestTemplate restTemplate(){RestTemplate restTemplate = new RestTemplate(getClientHttpRequestFactory());return restTemplate;}/*** 使用HttpClient作为底层客户端* @return*/private ClientHttpRequestFactory getClientHttpRequestFactory() {int timeout = 5000;RequestConfig config = RequestConfig.custom().setConnectTimeout(timeout).setConnectionRequestTimeout(timeout).setSocketTimeout(timeout).build();CloseableHttpClient client = HttpClientBuilder.create().setDefaultRequestConfig(config).build();return new HttpComponentsClientHttpRequestFactory(client);}
}

OkHttp 

/*** 使用OkHttpClient作为底层客户端* @return*/
private ClientHttpRequestFactory getClientHttpRequestFactory(){OkHttpClient okHttpClient = new OkHttpClient.Builder().connectTimeout(5, TimeUnit.SECONDS).writeTimeout(5, TimeUnit.SECONDS).readTimeout(5, TimeUnit.SECONDS).build();return new OkHttp3ClientHttpRequestFactory(okHttpClient);
}

 

使用

restTemplate 提供的api

1、设置Header和 body

// 创建 HttpEntity
String jsonBody = JSONObject.toJSONString(weChatRefundsRequestDto);
HttpEntity<String> entity = new HttpEntity<>(jsonBody, headers);ResponseEntity<WechatRefundsResponseDto> responseEntity =restTemplate.postForEntity("url",entity,WechatRefundsResponseDto.class);

其中在entity  中设置请求头和请求体;WechatRefundsResponseDto为设置接受返回信息的对象,其他postObject 类似以上方式

public <T> ResponseEntity<T> postForEntity(String url, @Nullable Object request,Class<T> responseType, Object... uriVariables) throws RestClientException

其中postObeject,postForEntity的请求方式中的request如果类型为HttpEntity则直接使用,不是就将request当成body放入HttpEntity中使用

2、get请求设置带参数的请求

@Autowired
private RestTemplate restTemplate;/*** 单元测试(带参的get请求)*/
@Test
public void testGetByRestFul(){//请求地址String url = "http://localhost:8080/testGetByRestFul/{1}/{2}";//发起请求,直接返回对象(restful风格)ResponseBean responseBean = restTemplate.getForObject(url, ResponseBean.class, "001", "张三");System.out.println(responseBean.toString());
}
@Autowired
private RestTemplate restTemplate;/*** 单元测试(带参的get请求)*/
@Test
public void testGetByParam(){//请求地址String url = "http://localhost:8080/testGetByParam?userName={userName}&userPwd={userPwd}";//请求参数Map<String, String> uriVariables = new HashMap<>();uriVariables.put("userName", "唐三藏");uriVariables.put("userPwd", "123456");//发起请求,直接返回对象(带参数请求)ResponseBean responseBean = restTemplate.getForObject(url, ResponseBean.class, uriVariables);System.out.println(responseBean.toString());
}

3、文件上传

@Autowired
private RestTemplate restTemplate;/*** 文件上传,post请求*/
@Test
public void upload(){//需要上传的文件String filePath = "/Users/panzhi/Desktop/Jietu20220205-194655.jpg";//请求地址String url = "http://localhost:8080/upload";// 请求头设置,multipart/form-data格式的数据HttpHeaders headers = new HttpHeaders();headers.setContentType(MediaType.MULTIPART_FORM_DATA);//提交参数设置MultiValueMap<String, Object> param = new LinkedMultiValueMap<>();param.add("uploadFile", new FileSystemResource(new File(filePath)));//服务端如果接受额外参数,可以传递param.add("userName", "张三");// 组装请求体HttpEntity<MultiValueMap<String, Object>> request = new HttpEntity<>(param, headers);//发起请求ResponseBean responseBean = restTemplate.postForObject(url, request, ResponseBean.class);System.out.println(responseBean.toString());
}

4、文件下载

@Autowired
private RestTemplate restTemplate;/*** 小文件下载* @throws IOException*/
@Test
public void downloadFile() throws IOException {String userName = "张三";String fileName = "c98b677c-0948-46ef-84d2-3742a2b821b0.jpg";//请求地址String url = "http://localhost:8080/downloadFile/{1}/{2}";//发起请求,直接返回对象(restful风格)ResponseEntity<byte[]> rsp = restTemplate.getForEntity(url, byte[].class, userName,fileName);System.out.println("文件下载请求结果状态码:" + rsp.getStatusCode());// 将下载下来的文件内容保存到本地String targetPath = "/Users/panzhi/Desktop/"  + fileName;Files.write(Paths.get(targetPath), Objects.requireNonNull(rsp.getBody(), "未获取到下载文件"));
}

 

参考:

REST 客户端 :: Spring Framework

Spring之RestTemplate详解-CSDN博客

http://www.hkea.cn/news/509210/

相关文章:

  • 提供网站建设费用资源网
  • wordpress怎么使用主题seo优化评论
  • 柳州做网站如何建网站详细步骤
  • 黄岛做网站哪家好四川seo关键词工具
  • dede门户网站模版写软文推广
  • 网站开发者排名开发一个app平台大概需要多少钱?
  • 做网站 博客百度推广助手客户端
  • 温州市手机网站制作哪家好爱站网长尾词挖掘
  • 党委网站建设要求凡科建站靠谱吗
  • wordpress 安卓客户端福建seo优化
  • 襄阳seo技术长沙seo网站优化
  • 做一的同志小说网站做seo要投入什么
  • 网站的文件结构百度搜索排名怎么收费
  • 全景网站app网络营销工具分析
  • 南京建设工程交易中心网站seo是什么的简称
  • 利用vps做网站关键字排名查询
  • 常熟网站制作找哪家好品牌型网站制作价格
  • 怎么做自己网站推广网络广告
  • 化州网站建设促销方法100种
  • 长沙专业网站设计平台新闻最新消息10条
  • 惠州网站建设制作宣传推广方案
  • 宁波网站推广外包服务长岭网站优化公司
  • 哈尔滨市哪里做淘宝网站seo课程心得体会
  • 做网站建设公司企业一个企业该如何进行网络营销
  • 移动端h5网站开发服务企业seo推广
  • 管理公司网站建设引擎搜索优化
  • 上市公司专利查询网站百度广告投放价格
  • html5电商网页制作网站怎么seo关键词排名优化推广
  • 大同网站建设黄冈网站推广优化找哪家
  • 昌邑网站建设站长之家网站排名