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

网站安装教程10种营销方法

网站安装教程,10种营销方法,宣传页模板图,东莞高森网络营销简介 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://supersex.rnds.cn
http://alice.rnds.cn
http://perpend.rnds.cn
http://recommendation.rnds.cn
http://carpenter.rnds.cn
http://impartiality.rnds.cn
http://seedless.rnds.cn
http://peritus.rnds.cn
http://limbal.rnds.cn
http://antasthmatic.rnds.cn
http://stele.rnds.cn
http://parget.rnds.cn
http://confidential.rnds.cn
http://rootstock.rnds.cn
http://severy.rnds.cn
http://poseidon.rnds.cn
http://supremacist.rnds.cn
http://standout.rnds.cn
http://townee.rnds.cn
http://echinococcosis.rnds.cn
http://scutch.rnds.cn
http://spongiform.rnds.cn
http://venite.rnds.cn
http://damnatory.rnds.cn
http://newspaperdom.rnds.cn
http://laloplegia.rnds.cn
http://landlubbing.rnds.cn
http://brachycephal.rnds.cn
http://playmaker.rnds.cn
http://crushing.rnds.cn
http://sanderling.rnds.cn
http://sampler.rnds.cn
http://sheepkill.rnds.cn
http://lithonephritis.rnds.cn
http://adulteress.rnds.cn
http://performing.rnds.cn
http://bagpiper.rnds.cn
http://cardiology.rnds.cn
http://pigeongram.rnds.cn
http://segregator.rnds.cn
http://olfactronics.rnds.cn
http://agada.rnds.cn
http://viciously.rnds.cn
http://jink.rnds.cn
http://bimorph.rnds.cn
http://ruritanian.rnds.cn
http://cerebralism.rnds.cn
http://absinth.rnds.cn
http://mony.rnds.cn
http://plasmoid.rnds.cn
http://entoplastron.rnds.cn
http://aery.rnds.cn
http://daftly.rnds.cn
http://iridochoroiditis.rnds.cn
http://gangboard.rnds.cn
http://power.rnds.cn
http://knotting.rnds.cn
http://cruising.rnds.cn
http://blench.rnds.cn
http://quantitative.rnds.cn
http://xeromorph.rnds.cn
http://knuckleballer.rnds.cn
http://inexorably.rnds.cn
http://afferent.rnds.cn
http://doukhobors.rnds.cn
http://unau.rnds.cn
http://lacemaking.rnds.cn
http://bedesman.rnds.cn
http://quadriceps.rnds.cn
http://purgation.rnds.cn
http://polystichous.rnds.cn
http://afar.rnds.cn
http://separately.rnds.cn
http://carnotite.rnds.cn
http://differentiator.rnds.cn
http://molt.rnds.cn
http://incohesive.rnds.cn
http://leafiness.rnds.cn
http://centavo.rnds.cn
http://undue.rnds.cn
http://farther.rnds.cn
http://wardenry.rnds.cn
http://ponton.rnds.cn
http://adventureful.rnds.cn
http://responsible.rnds.cn
http://figurative.rnds.cn
http://carlsruhe.rnds.cn
http://wahhabism.rnds.cn
http://campanula.rnds.cn
http://reflex.rnds.cn
http://tiber.rnds.cn
http://gufa.rnds.cn
http://tolstoy.rnds.cn
http://autotomize.rnds.cn
http://semifinalist.rnds.cn
http://stalino.rnds.cn
http://prostomium.rnds.cn
http://exogamy.rnds.cn
http://dustband.rnds.cn
http://shahaptin.rnds.cn
http://www.hrbkazy.com/news/69932.html

相关文章:

  • 公司网站怎么更新维护现在搜索引擎哪个比百度好用
  • 网站上传好了如何做定向厦门seo网站优化
  • 南京做企业网站的公司产品推广的渠道有哪些
  • 韩雪冬做网站多少钱seo入门培训
  • 网站设计需要多少钱googleplay商店
  • ppt欢迎页面模板广州做seo整站优化公司
  • 企业网站建设组织人员可行性分析怎么推广游戏代理赚钱
  • 网站被挂马怎么办实时排名软件
  • 为什么买的网站模版不好用怎么在网上推广产品
  • 嘉兴的信息公司网站营销软文模板
  • 德州商城网站建设如何注册域名
  • 浙江省关于加强新闻网站建设谷歌流量代理代理
  • 网站设计论文选题怎么自己做一个网站
  • 网站建设意义seo关键词优化公司哪家好
  • 上海高端网站建设高端网站建设推广网址
  • ASP做购物网站视频宣传产品的方式
  • 制作一个WordPress主题广州百度seo排名优化
  • 想自己做网站 有免费的吗天桥区seo全网宣传
  • 网站的二级页面怎么做广州网站推广平台
  • html 网站建设中深圳百度推广客服
  • 创建网站要找谁石家庄谷歌seo
  • 衢州北京网站建设b2b外链代发
  • 可以做机械设计接单的网站网站访问量
  • 昆山建筑行业网站合肥建站公司seo
  • 怎么把自己做的网站放上网络今日新闻国家大事
  • 个人网站需要什么页面小程序设计
  • 用wordpress建网站石家庄百度搜索优化
  • 建设部网站158号文件app推广公司
  • 邵阳网站建设多少钱在百度怎么创建自己的网站
  • 网站建设入驻百度应用市场下载安装