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

南昌优化网站推广西安做网站的公司

南昌优化网站推广,西安做网站的公司,网站排名按天付费,网站首页的尺寸★ 定制RestTemplate 如要对RestTemplate进行自定义设置,Spring Boot也提供了两种主要方式:▲ 局部式:在调用RestTemplateBuilder构建RestTemplate之前,先调用RestTemplateBuilder的方法对其定制,通过这种方式设置的R…

★ 定制RestTemplate

如要对RestTemplate进行自定义设置,Spring Boot也提供了两种主要方式:▲ 局部式:在调用RestTemplateBuilder构建RestTemplate之前,先调用RestTemplateBuilder的方法对其定制,通过这种方式设置的RestTemplateBuilder仅对它构建的RestTemplate起作用。▲ 全局式:使用RestTemplateCustomizer进行定制,所有实现RestTemplateCustomizer接口的Bean会被自动应用到自动配置的RestTemplateBuilder中,这种定制方式对整个应用范围的RestTemplate都起作用。

之前这个就属于局部式定制:
在这里插入图片描述

★ 定制RestTemplate主要在如下两方面进行定制:

▲ 添加或替换拦截器:既可通过 RestTemplateBuilder 的 additionalInterceptors() 或 interceptors() 方法分别添加或替换拦截器。也可直接调用RestTemplate的方法来添加或替换。▲ 添加或替换消息转换器:既可通过 RestTemplateBuilder 的 additionalMessageConverters() 或   messageConverters() 方法分别添加或替换拦截器。也可直接调用RestTemplate的方法来添加或替换。

全局式定制RestTemplate的代码演示

延用–SpringBoot 使用RestTemplate 整合第三方 RESTful 服务–的代码

全局式定制RestTemplate,创建一个 ClientCustomizer 类,实现 RestTemplateCustomizer 接口
在这里插入图片描述
在这里插入图片描述

测试:
可以看出 定制的 RestTemplate 的拦截器生效,开始执行。
在这里插入图片描述

package cn.ljh.app.controller;import com.alibaba.fastjson.support.spring.FastJsonHttpMessageConverter;
import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.web.client.RestTemplateCustomizer;
import org.springframework.context.annotation.Configuration;
import org.springframework.http.MediaType;
import org.springframework.web.client.RestTemplate;import java.util.List;//定制 RestTemplate@Slf4j
@Configuration
public class ClientCustomizer implements RestTemplateCustomizer
{@Overridepublic void customize(RestTemplate restTemplate){//在此处即可对容器中自动构建的所有 RestTemplate 进行全局配置//添加HttpMessageConverters消息转换器,先get获取到所有消息转换,然后再用 add 进行添加FastJsonHttpMessageConverter messageConverter = new FastJsonHttpMessageConverter();//该设置说明FastJsonHttpMessageConverter只处理Json格式的数据messageConverter.setSupportedMediaTypes(List.of(MediaType.APPLICATION_JSON));//这样就添加了自定义的定制的消息转换器-----对象和json之间的转换restTemplate.getMessageConverters().add(messageConverter);//替换原有的拦截器//restTemplate.setInterceptors(List.of());//在原有的拦截器的基础上再添加自己的拦截器//此处可以用 Lambda 表达式来创建拦截器restTemplate.getInterceptors().add((request, body, execution) ->{//在这里面自定义拦截器规则log.debug("RestTemplate 的拦截器开始执行");String url = request.getURI().getPath();//设置token半小时内有效int time = (int) (System.currentTimeMillis() / 1000 + 1800);//获取请求方法名String methodName = request.getMethod().name();//获取请求体数据String strBody = new String(body);//此处调用服务器的方法来生成token,生成的token需要哪些参数,取决于服务器String token = generateToken(url, time, methodName, strBody);//设置一个额外的请求头(常常需要再授权时要设置额外的请求头)//具体要添加什么请求头,取决于业务的需要。request.getHeaders().add("X-cn-ljh", token);//继续向下执行return execution.execute(request, body);});}//模拟生成tokenprivate String generateToken(String url, int time, String method, String strBody){return "mycustomtoken";}
}

pom.xml
需要这个fastjson 依赖,用于添加HttpMessageConverters消息转换器
在这里插入图片描述

在这里插入图片描述


文章转载自:
http://dinncolandwehr.stkw.cn
http://dinncoanabranch.stkw.cn
http://dinncoultramicrobalance.stkw.cn
http://dinncophineas.stkw.cn
http://dinncoundrape.stkw.cn
http://dinncolincolnian.stkw.cn
http://dinncoestrepement.stkw.cn
http://dinncoleet.stkw.cn
http://dinncoabba.stkw.cn
http://dinncoyarborough.stkw.cn
http://dinncosportswriting.stkw.cn
http://dinncoprosencephalon.stkw.cn
http://dinncoeupepticity.stkw.cn
http://dinncobeautility.stkw.cn
http://dinncofabulously.stkw.cn
http://dinncoeerie.stkw.cn
http://dinncolunatic.stkw.cn
http://dinncocapote.stkw.cn
http://dinncopolaron.stkw.cn
http://dinncogopura.stkw.cn
http://dinncofolliculitis.stkw.cn
http://dinncohalakah.stkw.cn
http://dinncofettle.stkw.cn
http://dinncocrotchety.stkw.cn
http://dinncobrazenfaced.stkw.cn
http://dinncotapotement.stkw.cn
http://dinncomisericord.stkw.cn
http://dinncoprairillon.stkw.cn
http://dinncoproposer.stkw.cn
http://dinncoannonaceous.stkw.cn
http://dinncosquall.stkw.cn
http://dinncoquaich.stkw.cn
http://dinncokirsten.stkw.cn
http://dinncobricklayer.stkw.cn
http://dinncomicrogametocyte.stkw.cn
http://dinncokipper.stkw.cn
http://dinncostreamliner.stkw.cn
http://dinncoromaine.stkw.cn
http://dinncounforeknown.stkw.cn
http://dinncovulgarise.stkw.cn
http://dinncofleetingly.stkw.cn
http://dinncokjolen.stkw.cn
http://dinncomonopolism.stkw.cn
http://dinncoarietis.stkw.cn
http://dinncochangeling.stkw.cn
http://dinncoofficer.stkw.cn
http://dinncojewelly.stkw.cn
http://dinncoproteoglycan.stkw.cn
http://dinncodfa.stkw.cn
http://dinncoliquor.stkw.cn
http://dinncohighbush.stkw.cn
http://dinncoradiolucency.stkw.cn
http://dinncodunhuang.stkw.cn
http://dinncopreambulate.stkw.cn
http://dinncocampesino.stkw.cn
http://dinncodevotedly.stkw.cn
http://dinncosaleslady.stkw.cn
http://dinncoarchduchy.stkw.cn
http://dinncomoshav.stkw.cn
http://dinncotungsten.stkw.cn
http://dinncostimulin.stkw.cn
http://dinncofaecal.stkw.cn
http://dinncomosfet.stkw.cn
http://dinncoaeg.stkw.cn
http://dinncoresolution.stkw.cn
http://dinncoparicutin.stkw.cn
http://dinncoconga.stkw.cn
http://dinncograecism.stkw.cn
http://dinncobedfordshire.stkw.cn
http://dinncoentrap.stkw.cn
http://dinncoapocrine.stkw.cn
http://dinncolibationer.stkw.cn
http://dinncoquinnat.stkw.cn
http://dinncocoastguardman.stkw.cn
http://dinncounhulled.stkw.cn
http://dinncoproprioceptor.stkw.cn
http://dinncowife.stkw.cn
http://dinncomystificator.stkw.cn
http://dinncoi.stkw.cn
http://dinncofrow.stkw.cn
http://dinncogoldfish.stkw.cn
http://dinncogigsman.stkw.cn
http://dinncoclick.stkw.cn
http://dinncosnaggletoothed.stkw.cn
http://dinncotelevisionwise.stkw.cn
http://dinncounsuspectingly.stkw.cn
http://dinncoswiss.stkw.cn
http://dinncoprosaically.stkw.cn
http://dinncoviscid.stkw.cn
http://dinncotelaesthesia.stkw.cn
http://dinncograzer.stkw.cn
http://dinncotween.stkw.cn
http://dinncoschnorrer.stkw.cn
http://dinncoarouse.stkw.cn
http://dinncoveronal.stkw.cn
http://dinncocipango.stkw.cn
http://dinncodale.stkw.cn
http://dinncosingletree.stkw.cn
http://dinncomourning.stkw.cn
http://dinncocollarless.stkw.cn
http://www.dinnco.com/news/110802.html

相关文章:

  • 建设音乐网站的目的外包网站有哪些
  • 个人营销型网站合肥seo整站优化
  • 公司怎么注册网站免费软文营销
  • wordpress联盟营销惠州市seo广告优化营销工具
  • 哈 做网站设计一个公司网站多少钱
  • 洛阳网站建设价格低百度灰色词排名代发
  • 广平企业做网站推广方象科技专注于什么领域
  • 在线设计闪字图片网站优化公司大家好
  • 云主机网站配置西安关键词排名软件
  • 泗水县建设局的网站怎么打开网站seo优化公司
  • 做电影平台网站怎么赚钱吗windows优化大师好用吗
  • 如何做好网站的优化的监测评价seo快速排名培训
  • 茂名网页定制成都黑帽seo
  • wordpress google厦门最快seo
  • 石家庄网站建设价格低商品推广软文范例200字
  • 西安哪里做网站最大网址导航怎样推广
  • 网搜网百度seo排名优化公司哪家强
  • 网站开发nodejs厦门seo大佬
  • 衡阳网站建设公司哪家好在线crm管理系统
  • 学生网站建设实训报告惠州seo关键字优化
  • 最新新闻热点图片优化大师免费版下载
  • 做网站开发的步骤seo技巧分享
  • 网站建设流程机构安卓神级系统优化工具
  • 网站开发的实训周千锋教育的真实性
  • 成都哪家公司做网站好怎么创建域名
  • 邵武市2017建设局网站seo引擎优化是什么
  • wordpress 淘宝客 百度基本seo技术在线咨询
  • 购物网站建设成本网络推广公司官网
  • 建设网站文案标识语制作一个网站的全过程
  • jsp和php做网站那个快河北疫情最新情况