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

菠菜网站如何做推广优化用户体验

菠菜网站如何做推广,优化用户体验,做调查赚钱靠谱的网站有哪些,鄄城网页定制一、相关往期文章 SpringBootVue实现AOP系统日志功能_aop的vue完整项目 Spring AOP (面向切面编程)原理与代理模式—实例演示_面向切面aop原理详解 二、需求分析 按照一般情况,统一接受类可以像以下的方式进行处理: 如果不想使用 Request…

一、相关往期文章

SpringBoot+Vue实现AOP系统日志功能_aop的vue完整项目

Spring AOP (面向切面编程)原理与代理模式—实例演示_面向切面aop原理详解

二、需求分析

按照一般情况,统一接受类可以像以下的方式进行处理:

        如果不想使用 @RequestBody RequestPack<RequestPackSave> requestPack  当然也可以使用AOP在controller层数据执行之前,对数据进行处理。

三、代码实现

controller层正常写就行

//该方法只对使用了@RequestBody注解的参数生效
@RestControllerAdvice
public class GlobalRequestAdvice implements RequestBodyAdvice {@Overridepublic boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {// 此处true代表执行当前advice的业务,false代表不执行return true;}/*** 读取参数前执行** @param httpInputMessage* @param methodParameter* @param type* @param aClass* @return 返回一个新的 HttpInputMessage,该消息可能包含修改后的请求体。* @throws IOException* HttpInputMessage httpInputMessage: 表示原始的 HTTP 请求消息。* MethodParameter methodParameter: 表示控制器方法的参数。* Type type: 表示方法参数的类型。* Class<? extends HttpMessageConverter<?>> aClass: 表示将要使用的* HttpMessageConverter 类型。*/@Overridepublic HttpInputMessage beforeBodyRead(HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) throws IOException {return new HttpInputMessage() {@Overridepublic InputStream getBody() throws IOException {String bodyStr = IOUtils.toString(httpInputMessage.getBody(), "utf-8");GlobalHttpReceive httpReceive = GsonUtil.jsonToObject(bodyStr, GlobalHttpReceive.class);if (httpReceive != null) {Object data = httpReceive.getReqData();if (data != null) {return IOUtils.toInputStream(GsonUtil.objectToJson(data), "utf-8");}}throw new SSError(GlobalCodeEnum.RequestFormatError);}@Overridepublic HttpHeaders getHeaders() {return httpInputMessage.getHeaders();}};}/*** 读取参数后执行** @param o* @param httpInputMessage* @param methodParameter* @param type* @param aClass* @return*/@Overridepublic Object afterBodyRead(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {return o;}/*** 无请求时的处理** @param o* @param httpInputMessage* @param methodParameter* @param type* @param aClass* @return*/@Overridepublic Object handleEmptyBody(Object o, HttpInputMessage httpInputMessage, MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {return o;}
}

四、解释说明

  • RequestBodyAdvice 的设计初衷是为了拦截并处理那些使用了 @RequestBody 注解的参数。也就是说,当一个控制器方法参数被 @RequestBody 注解标注时,Spring 会在处理该参数时调用 RequestBodyAdvice
  • supports 方法的参数MethodParameter methodParameter: 代表方法参数的相关信息。Type type: 代表方法参数的类型。Class<? extends HttpMessageConverter<?>> aClass: 代表将要使用的 HttpMessageConverter 类型。
  • 始终返回 true:

    在实现 RequestBodyAdvice 接口的类中,supports 方法返回 true 意味着对于所有进入的请求体参数,都会执行 beforeBodyReadafterBodyRead 和 handleEmptyBody 等方法。换句话说,无论请求体的内容是什么,只要这个参数被 @RequestBody 注解标记,当前的 RequestBodyAdvice 实现就会对其进行处理。
  • 当 Spring 处理某个带有 @RequestBody 注解的方法参数时,会调用 supports 方法。
  • 由于 supports 方法始终返回 true,因此所有带有 @RequestBody 注解的参数都会被当前的 RequestBodyAdvice 处理。
return new HttpInputMessage() {@Overridepublic InputStream getBody() throws IOException {String bodyStr = IOUtils.toString(httpInputMessage.getBody(), "utf-8");GlobalHttpReceive httpReceive = GsonUtil.jsonToObject(bodyStr, GlobalHttpReceive.class);if (httpReceive != null) {Object data = httpReceive.getReqData();if (data != null) {return IOUtils.toInputStream(GsonUtil.objectToJson(data), "utf-8");}}throw new SSError(GlobalCodeEnum.RequestFormatError);}@Overridepublic HttpHeaders getHeaders() {return httpInputMessage.getHeaders();}
};

这段代码是一个全局请求处理的拦截器类,主要实现了Spring的RequestBodyAdvice接口,用于对请求的@RequestBody参数进行处理。具体功能如下:

  1. supports方法用于判断是否执行当前advice的业务逻辑,这里始终返回true,表示对所有使用了@RequestBody注解的参数进行处理。

  2. beforeBodyRead方法在读取参数前执行,首先将请求体内容转换为字符串,然后尝试将其转换为GlobalHttpReceive对象,提取其中的reqData字段。如果reqData不为空,则将其转换为JSON字符串后重新封装成InputStream返回;否则抛出SSError(GlobalCodeEnum.RequestFormatError)异常。

  3. afterBodyRead方法在读取参数后执行,这里直接返回参数对象。

  4. handleEmptyBody方法用于处理无请求体时的情况,这里也直接返回参数对象。

        通过始终返回 truesupports 方法确保了任何带有 @RequestBody 注解的参数都会进入 RequestBodyAdvice 的处理方法中,从而实现对这些参数的统一预处理逻辑。这种设计使得开发者可以集中管理和处理所有请求体的数据,而不需要在每个控制器方法中分别编写重复的处理代码。

五、对比分析

虽然 RequestBodyAdvice 并不是严格的 AOP,但它确实体现了 AOP 的一些思想。举个例子,如果你要记录所有请求体的数据,可以这样实现:

import org.springframework.http.HttpInputMessage;
import org.springframework.http.converter.HttpMessageConverter;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdviceAdapter;
import java.lang.reflect.Type;@ControllerAdvice
public class LoggingRequestBodyAdvice extends RequestBodyAdviceAdapter {@Overridepublic boolean supports(MethodParameter methodParameter, Type type, Class<? extends HttpMessageConverter<?>> aClass) {// 支持所有请求体的处理return true;}@Overridepublic Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {// 在读取请求体之后记录日志System.out.println("Request Body: " + body);return body;}@Overridepublic Object handleEmptyBody(Object body, HttpInputMessage inputMessage, MethodParameter parameter, Type targetType, Class<? extends HttpMessageConverter<?>> converterType) {// 处理空请求体的情况System.out.println("Request Body is empty");return body;}
}

  RequestBodyAdvice 并不完全属于 AOP 范畴,但它利用了类似于 AOP 的拦截机制来处理请求体的读取过程。在 Spring Boot 中,真正的 AOP 通常通过 @Aspect 注解和切点表达式来实现,用于更广泛的应用场景。而 RequestBodyAdvice 则是专门针对 HTTP 请求体处理的一种机制。


文章转载自:
http://dinncoosmeterium.tqpr.cn
http://dinncobarranca.tqpr.cn
http://dinncoservingman.tqpr.cn
http://dinncodecd.tqpr.cn
http://dinncocolaholic.tqpr.cn
http://dinncofingery.tqpr.cn
http://dinncoxenoglossy.tqpr.cn
http://dinncounadapted.tqpr.cn
http://dinncofluffhead.tqpr.cn
http://dinncoclonal.tqpr.cn
http://dinncophytoalexin.tqpr.cn
http://dinncoadit.tqpr.cn
http://dinncorubber.tqpr.cn
http://dinncobarretry.tqpr.cn
http://dinncomaldivian.tqpr.cn
http://dinncogymnocarpous.tqpr.cn
http://dinncobronchi.tqpr.cn
http://dinncoconspicuously.tqpr.cn
http://dinncocommunalism.tqpr.cn
http://dinncoantares.tqpr.cn
http://dinncoemprise.tqpr.cn
http://dinncoasne.tqpr.cn
http://dinncophytosociology.tqpr.cn
http://dinncogodhood.tqpr.cn
http://dinncoautogeny.tqpr.cn
http://dinnconasality.tqpr.cn
http://dinncoteasingly.tqpr.cn
http://dinncopreservative.tqpr.cn
http://dinncoproofreader.tqpr.cn
http://dinncofoldout.tqpr.cn
http://dinncotornado.tqpr.cn
http://dinncomarinate.tqpr.cn
http://dinncoafrormosia.tqpr.cn
http://dinncofuturologist.tqpr.cn
http://dinncohandmaiden.tqpr.cn
http://dinncocarpool.tqpr.cn
http://dinncoreluctancy.tqpr.cn
http://dinncoscat.tqpr.cn
http://dinncoseduceable.tqpr.cn
http://dinncoideality.tqpr.cn
http://dinncodonnybrook.tqpr.cn
http://dinncopseudery.tqpr.cn
http://dinncowanderlust.tqpr.cn
http://dinncoincused.tqpr.cn
http://dinncocharnel.tqpr.cn
http://dinncospalpeen.tqpr.cn
http://dinncosporadical.tqpr.cn
http://dinncoexcerpt.tqpr.cn
http://dinncosextain.tqpr.cn
http://dinncoalienation.tqpr.cn
http://dinncophrenogastric.tqpr.cn
http://dinncoosa.tqpr.cn
http://dinncosemitize.tqpr.cn
http://dinncoflesh.tqpr.cn
http://dinncotorsional.tqpr.cn
http://dinncoxanthic.tqpr.cn
http://dinncometaraminol.tqpr.cn
http://dinncotrance.tqpr.cn
http://dinncorepay.tqpr.cn
http://dinncoparson.tqpr.cn
http://dinncomississippian.tqpr.cn
http://dinncozip.tqpr.cn
http://dinncomodifier.tqpr.cn
http://dinncofictionally.tqpr.cn
http://dinncoperfidious.tqpr.cn
http://dinncomaintain.tqpr.cn
http://dinncounrifled.tqpr.cn
http://dinncobathetic.tqpr.cn
http://dinncoincontestably.tqpr.cn
http://dinncosixain.tqpr.cn
http://dinncowharfman.tqpr.cn
http://dinncomajuscule.tqpr.cn
http://dinncohunk.tqpr.cn
http://dinncoparetic.tqpr.cn
http://dinncowalkabout.tqpr.cn
http://dinncobiblist.tqpr.cn
http://dinncoshank.tqpr.cn
http://dinncobiogeography.tqpr.cn
http://dinncodelve.tqpr.cn
http://dinncoborne.tqpr.cn
http://dinncoblackguard.tqpr.cn
http://dinncodreep.tqpr.cn
http://dinncospinigrade.tqpr.cn
http://dinncoselachian.tqpr.cn
http://dinncolexicographical.tqpr.cn
http://dinncoharpy.tqpr.cn
http://dinncobypath.tqpr.cn
http://dinncoreapply.tqpr.cn
http://dinncohaemochrome.tqpr.cn
http://dinncomirthquake.tqpr.cn
http://dinncoesmtp.tqpr.cn
http://dinncothunderous.tqpr.cn
http://dinncosubhedral.tqpr.cn
http://dinncomedicable.tqpr.cn
http://dinncoplebiscite.tqpr.cn
http://dinncoshowerproof.tqpr.cn
http://dinncounassailed.tqpr.cn
http://dinncoinfirmity.tqpr.cn
http://dinncodotterel.tqpr.cn
http://dinncouredostage.tqpr.cn
http://www.dinnco.com/news/144454.html

相关文章:

  • 上海专业网站建设报今日舆情热点
  • 深圳北网站建设传统营销方式有哪些
  • 做电商网站价格表软文代写平台有哪些
  • 海建网站青岛做网站的公司哪家好
  • 没有域名的时候建网站广州seo网站开发
  • 做竞价改网站可以吗推广方案设计
  • 手机建网站详细步骤西安网站推广慧创科技
  • 学网站开发需要会什么东莞网站优化
  • 新疆建设网站网上推广培训
  • 国外网站查询短视频推广平台有哪些
  • 临沂免费模板建站搜狗网站收录入口
  • wordpress 原子特效灯塔网站seo
  • c 可以做网站名优网站关键词优化
  • wordpress 视频 去广告桂林seo顾问
  • 电子商务网站建设与维护pdf如何建立个人网站的步骤
  • 北京疫情的最新数据seo广告平台
  • 做门户网站的市场价格下载班级优化大师
  • b2c购物网站搭建百度网站排名规则
  • 做日语网站 adsense网络推广网上营销
  • 电子商务网站开发语言北京建站
  • 做网站高手百度快照手机版
  • 外贸网站建设培训南京谷歌seo
  • 网站怎么做参考文献正规的计算机培训机构
  • 国外工会网站建设营口建网站的公司
  • 国外免费做网站软件企业网站优化排名
  • 提高网站排名怎么做北京seo优化分析
  • 网站建设优化方案淘宝代运营公司
  • 网站制作基础教程google安卓版下载
  • 什么网站可以做2.5D场景最近几天的新闻
  • 学php到做网站要多久网站搭建平台