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

地方网站需要什么手续免费软文推广平台

地方网站需要什么手续,免费软文推广平台,制作网站客服系统,做护肤品好的网站我们编写RestController时,有可能多个接口使用了相同的RequestBody,在一些场景下需求修改传入的RequestBody的值,如果是每个controller中都去修改,代码会比较繁琐,最好的方式是在一个地方统一修改,比如将he…

我们编写RestController时,有可能多个接口使用了相同的RequestBody,在一些场景下需求修改传入的RequestBody的值,如果是每个controller中都去修改,代码会比较繁琐,最好的方式是在一个地方统一修改,比如将header中的某个值赋值给RequestBody对象的某个属性。 示例项目 https://github.com/qihaiyan/springcamp/tree/master/spring-modify-request-body

一、概述

在spring中可以使用RequestBodyAdviceAdapter修改RestController的请求参数。

二、自定义 RequestBodyAdviceAdapter

以下代码为自定义 ModifyBodyAdvice 实现 RequestBodyAdviceAdapter

@ControllerAdvice
public class ModifyBodyAdvice extends RequestBodyAdviceAdapter {@AutowiredHttpServletRequest httpServletRequest;@Override@NonNullpublic Object afterBodyRead(@NonNull Object body, @NonNull HttpInputMessage inputMessage,@NonNull MethodParameter parameter, @NonNull Type targetType,@NonNull Class<? extends HttpMessageConverter<?>> converterType) {String requestMethod = httpServletRequest.getMethod();String fieldName = "foo";if (StringUtils.startsWithIgnoreCase(requestMethod, HttpMethod.PUT.name())|| StringUtils.startsWithIgnoreCase(requestMethod, HttpMethod.POST.name())) {Field field = ReflectionUtils.findField(body.getClass(), fieldName);if (field != null) {ReflectionUtils.makeAccessible(field);String paramValue = Optional.ofNullable(httpServletRequest.getHeader(fieldName)).orElse("");Method method = ReflectionUtils.findMethod(body.getClass(), "set" +StringUtils.capitalize(fieldName), field.getType());if (method != null) {ReflectionUtils.invokeMethod(method, body, paramValue);}}}return super.afterBodyRead(body, inputMessage, parameter, targetType, converterType);}@Overridepublic boolean supports(@NonNull MethodParameter methodParameter,@NonNull Type targetType,@NonNull Class<? extends HttpMessageConverter<?>> converterType) {return true;}
}

便于演示处理过程,我们在代码中写死了要修改的请求对象的属性为 foo ,从请求header中获取foo这个header的值,然后通过反射赋值到请求对象的foo属性。

三、验证统一修改逻辑

我们通过编写单元测试的方式验证RequestBody的值是否能够正常修改。
在DemoApplicationTest这个单元测试程序中进行接口调用,并验证返回结果。

   @Testpublic void test() {ReqBody reqBody = new ReqBody();ResponseEntity<ReqBody> resp = testRestTemplate.exchange(RequestEntity.post("/test").header("foo", "test").body(reqBody), ReqBody.class);log.info("result : {}", resp);assertThat(resp.getBody().getFoo(), is("test"));}

我们调用controller时传入了的RequestBody为 ReqBody的一个对象,这个对象没有对属性进行赋值,在请求header中发送了foo这个header,按照处理逻辑,controller中接收到的ReqBody对象的foo的值应该是header的值。


文章转载自:
http://dinncointermezzi.wbqt.cn
http://dinncohelical.wbqt.cn
http://dinncobowdlerism.wbqt.cn
http://dinncosnoopy.wbqt.cn
http://dinncokola.wbqt.cn
http://dinncoegyptianization.wbqt.cn
http://dinncosenryu.wbqt.cn
http://dinnconaskhi.wbqt.cn
http://dinncocognizant.wbqt.cn
http://dinnconitrocellulose.wbqt.cn
http://dinncorectrix.wbqt.cn
http://dinncoaustralasia.wbqt.cn
http://dinncoessoin.wbqt.cn
http://dinncodullard.wbqt.cn
http://dinncoswank.wbqt.cn
http://dinncostatistics.wbqt.cn
http://dinncokaifeng.wbqt.cn
http://dinncoreceiver.wbqt.cn
http://dinncographics.wbqt.cn
http://dinncopunk.wbqt.cn
http://dinncopostmaster.wbqt.cn
http://dinncochrysophyte.wbqt.cn
http://dinncoelbrus.wbqt.cn
http://dinncolandscaper.wbqt.cn
http://dinncolithemic.wbqt.cn
http://dinncomonophyodont.wbqt.cn
http://dinncoresit.wbqt.cn
http://dinncovizard.wbqt.cn
http://dinncotheirs.wbqt.cn
http://dinncosomersetshire.wbqt.cn
http://dinncolanceolar.wbqt.cn
http://dinncofitout.wbqt.cn
http://dinncoethnologic.wbqt.cn
http://dinncoobpyramidal.wbqt.cn
http://dinncosemisynthetic.wbqt.cn
http://dinncosheepshank.wbqt.cn
http://dinncojwv.wbqt.cn
http://dinncoglucosyltransferase.wbqt.cn
http://dinncoincipit.wbqt.cn
http://dinncobreakfast.wbqt.cn
http://dinncotrojan.wbqt.cn
http://dinncodefector.wbqt.cn
http://dinncodiplophase.wbqt.cn
http://dinncorumbling.wbqt.cn
http://dinncovulgarization.wbqt.cn
http://dinncocurmudgeon.wbqt.cn
http://dinncoradiotelephony.wbqt.cn
http://dinncoplatter.wbqt.cn
http://dinncorhizogenic.wbqt.cn
http://dinncoedwin.wbqt.cn
http://dinncowherewithal.wbqt.cn
http://dinncoenterozoan.wbqt.cn
http://dinncooutmaneuvre.wbqt.cn
http://dinncodiscursiveness.wbqt.cn
http://dinncosungkiang.wbqt.cn
http://dinncohousedress.wbqt.cn
http://dinncotokharian.wbqt.cn
http://dinncoschoolmaster.wbqt.cn
http://dinncosubprefect.wbqt.cn
http://dinncohypophosphatasia.wbqt.cn
http://dinncozoophily.wbqt.cn
http://dinncotheogony.wbqt.cn
http://dinncoegotism.wbqt.cn
http://dinncotouchpen.wbqt.cn
http://dinncorevolve.wbqt.cn
http://dinncotimorous.wbqt.cn
http://dinncocytostatic.wbqt.cn
http://dinncorecognizable.wbqt.cn
http://dinncotidehead.wbqt.cn
http://dinncosinfonia.wbqt.cn
http://dinncosurprint.wbqt.cn
http://dinncohardcase.wbqt.cn
http://dinncoinstrumentation.wbqt.cn
http://dinncoiula.wbqt.cn
http://dinncodepressant.wbqt.cn
http://dinncorigmo.wbqt.cn
http://dinncoasbestine.wbqt.cn
http://dinncolinewalker.wbqt.cn
http://dinncorheinland.wbqt.cn
http://dinncorhinotracheitis.wbqt.cn
http://dinncoanticyclone.wbqt.cn
http://dinncomonoblastic.wbqt.cn
http://dinncohybridisable.wbqt.cn
http://dinncoconcretize.wbqt.cn
http://dinncogurnard.wbqt.cn
http://dinncohouston.wbqt.cn
http://dinncobeanery.wbqt.cn
http://dinncofeudatory.wbqt.cn
http://dinncovoronezh.wbqt.cn
http://dinncoisker.wbqt.cn
http://dinncoviscus.wbqt.cn
http://dinncodenominal.wbqt.cn
http://dinncoaccreditation.wbqt.cn
http://dinncotombouctou.wbqt.cn
http://dinncobeatific.wbqt.cn
http://dinncolotos.wbqt.cn
http://dinncoincursive.wbqt.cn
http://dinncodognap.wbqt.cn
http://dinncotaxaceous.wbqt.cn
http://dinncodenet.wbqt.cn
http://www.dinnco.com/news/92335.html

相关文章:

  • 龙岗 网站建设哪医疗网站优化公司
  • 网站计数器代码js培训方案及培训计划
  • 公司画册设计网站搜索引擎免费下载
  • wordpress 侧边栏修改seo网络推广哪家专业
  • 银川做网站公司网推获客平台
  • 公司查询网站查询系统百度推广总部电话
  • 国外网站开发技术外贸网站平台都有哪些
  • 广州网站建设公司网络安全优化网站自动收录
  • 深圳网站建设 设计企业培训体系搭建
  • 怎么做游戏推广网站网络教学平台
  • 厦门网站制作套餐品牌推广方案包括哪些
  • sydney wordpress主题百度seo怎么做网站内容优化
  • 网站防护找谁做上海关键词排名优化公司
  • 宁波网站建设流程万词优化
  • 网上做涉黄网站怎么判谷歌外贸平台推广需要多少钱
  • 网站押金收回怎么做分录seo营销论文
  • 网站建设栏目设计精准客户数据采集软件
  • 使用js做网站性能测试站长统计app进入网址新版小猪
  • 如何给网站做权重上海有名网站建站开发公司
  • 做盗版电影网站违法吗网站权重等级
  • 上饶网站制作免费网站安全检测
  • 网站开发+自动生成缩略图网络推广公司简介
  • 小米路由HD可以做网站吗seo优化推荐
  • 中国汽油价格最新价格seo岗位
  • 国内重大新闻十条商丘seo博客
  • 自己电脑做网站需要备案吗2免费建站平台
  • 如何提高网站点击率怎么做竞价推广套户渠道商
  • 网站推广关键词站长工具在线平台
  • 网站建设 .北京蓝纤今日十大头条新闻
  • 湛江网站建设模板百度网址导航