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

武汉微信网站制作网站流量查询

武汉微信网站制作,网站流量查询,怎么做sf网站,传奇手游在线玩网页游戏一个请求在Spring中处理流程是有多种方式拦截处理的,而且,请求是可以拆分为进入和响应2个操作的,进入我们通常会对请求参数做处理,而响应我们通常会对响应参数做处理,Spring提供了多种方式给开发者。 一、HandlerInte…

一个请求在Spring中处理流程是有多种方式拦截处理的,而且,请求是可以拆分为进入和响应2个操作的,进入我们通常会对请求参数做处理,而响应我们通常会对响应参数做处理,Spring提供了多种方式给开发者。

一、HandlerInterceptor

我们写的controller,在Spring中被定义为handler,拦截controller的拦截器被定义为org.springframework.web.servlet.HandlerInterceptor。

拦截器的拦截逻辑是在org.springframework.web.servlet.DispatcherServlet中写的,需要注意的是,如果入口拦截顺序是a->b->c的话,那么出口拦截顺序是c->b->a,这个逻辑可以看org.springframework.web.servlet.HandlerExecutionChain里的一段逻辑。

	protected void doDispatch(HttpServletRequest request, HttpServletResponse response) throws Exception {HttpServletRequest processedRequest = request;HandlerExecutionChain mappedHandler = null;boolean multipartRequestParsed = false;WebAsyncManager asyncManager = WebAsyncUtils.getAsyncManager(request);try {ModelAndView mv = null;Exception dispatchException = null;try {processedRequest = checkMultipart(request);multipartRequestParsed = (processedRequest != request);// Determine handler for the current request.mappedHandler = getHandler(processedRequest);if (mappedHandler == null) {noHandlerFound(processedRequest, response);return;}// Determine handler adapter for the current request.HandlerAdapter ha = getHandlerAdapter(mappedHandler.getHandler());// Process last-modified header, if supported by the handler.String method = request.getMethod();boolean isGet = HttpMethod.GET.matches(method);if (isGet || HttpMethod.HEAD.matches(method)) {long lastModified = ha.getLastModified(request, mappedHandler.getHandler());if (new ServletWebRequest(request, response).checkNotModified(lastModified) && isGet) {return;}}if (!mappedHandler.applyPreHandle(processedRequest, response)) {return;}// Actually invoke the handler.mv = ha.handle(processedRequest, response, mappedHandler.getHandler());if (asyncManager.isConcurrentHandlingStarted()) {return;}applyDefaultViewName(processedRequest, mv);mappedHandler.applyPostHandle(processedRequest, response, mv);}catch (Exception ex) {dispatchException = ex;}catch (Throwable err) {// As of 4.3, we're processing Errors thrown from handler methods as well,// making them available for @ExceptionHandler methods and other scenarios.dispatchException = new NestedServletException("Handler dispatch failed", err);}processDispatchResult(processedRequest, response, mappedHandler, mv, dispatchException);}catch (Exception ex) {triggerAfterCompletion(processedRequest, response, mappedHandler, ex);}catch (Throwable err) {triggerAfterCompletion(processedRequest, response, mappedHandler,new NestedServletException("Handler processing failed", err));}finally {if (asyncManager.isConcurrentHandlingStarted()) {// Instead of postHandle and afterCompletionif (mappedHandler != null) {mappedHandler.applyAfterConcurrentHandlingStarted(processedRequest, response);}}else {// Clean up any resources used by a multipart request.if (multipartRequestParsed) {cleanupMultipart(processedRequest);}}}}

这里能很清晰的看到循环使用的次序。 

	boolean applyPreHandle(HttpServletRequest request, HttpServletResponse response) throws Exception {for (int i = 0; i < this.interceptorList.size(); i++) {HandlerInterceptor interceptor = this.interceptorList.get(i);if (!interceptor.preHandle(request, response, this.handler)) {triggerAfterCompletion(request, response, null);return false;}this.interceptorIndex = i;}return true;}void applyPostHandle(HttpServletRequest request, HttpServletResponse response, @Nullable ModelAndView mv)throws Exception {for (int i = this.interceptorList.size() - 1; i >= 0; i--) {HandlerInterceptor interceptor = this.interceptorList.get(i);interceptor.postHandle(request, response, this.handler, mv);}}

 org.springframework.web.servlet.HandlerInterceptor

public interface HandlerInterceptor {/*** Interception point before the execution of a handler. Called after* HandlerMapping determined an appropriate handler object, but before* HandlerAdapter invokes the handler.* <p>DispatcherServlet processes a handler in an execution chain, consisting* of any number of interceptors, with the handler itself at the end.* With this method, each interceptor can decide to abort the execution chain,* typically sending an HTTP error or writing a custom response.* <p><strong>Note:</strong> special considerations apply for asynchronous* request processing. For more details see* {@link org.springframework.web.servlet.AsyncHandlerInterceptor}.* <p>The default implementation returns {@code true}.* @param request current HTTP request* @param response current HTTP response* @param handler chosen handler to execute, for type and/or instance evaluation* @return {@code true} if the execution chain should proceed with the* next interceptor or the handler itself. Else, DispatcherServlet assumes* that this interceptor has already dealt with the response itself.* @throws Exception in case of errors*/default boolean preHandle(HttpServletRequest request, HttpServletResponse response, Object handler)throws Exception {return true;}/*** Interception point after successful execution of a handler.* Called after HandlerAdapter actually invoked the handler, but before the* DispatcherServlet renders the view. Can expose additional model objects* to the view via the given ModelAndView.* <p>DispatcherServlet processes a handler in an execution chain, consisting* of any number of interceptors, with the handler itself at the end.* With this method, each interceptor can post-process an execution,* getting applied in inverse order of the execution chain.* <p><strong>Note:</strong> special considerations apply for asynchronous* request processing. For more details see* {@link org.springframework.web.servlet.AsyncHandlerInterceptor}.* <p>The default implementation is empty.* @param request current HTTP request* @param response current HTTP response* @param handler the handler (or {@link HandlerMethod}) that started asynchronous* execution, for type and/or instance examination* @param modelAndView the {@code ModelAndView} that the handler returned* (can also be {@code null})* @throws Exception in case of errors*/default void postHandle(HttpServletRequest request, HttpServletResponse response, Object handler,@Nullable ModelAndView modelAndView) throws Exception {}/*** Callback after completion of request processing, that is, after rendering* the view. Will be called on any outcome of handler execution, thus allows* for proper resource cleanup.* <p>Note: Will only be called if this interceptor's {@code preHandle}* method has successfully completed and returned {@code true}!* <p>As with the {@code postHandle} method, the method will be invoked on each* interceptor in the chain in reverse order, so the first interceptor will be* the last to be invoked.* <p><strong>Note:</strong> special considerations apply for asynchronous* request processing. For more details see* {@link org.springframework.web.servlet.AsyncHandlerInterceptor}.* <p>The default implementation is empty.* @param request current HTTP request* @param response current HTTP response* @param handler the handler (or {@link HandlerMethod}) that started asynchronous* execution, for type and/or instance examination* @param ex any exception thrown on handler execution, if any; this does not* include exceptions that have been handled through an exception resolver* @throws Exception in case of errors*/default void afterCompletion(HttpServletRequest request, HttpServletResponse response, Object handler,@Nullable Exception ex) throws Exception {}}

二、HandlerMethodArgumentResolver

上面的HandlerInterceptor可以清楚的看到接收的参数是HttpServletRequest,这是最早期的参数,紧接着Spring会从HttpServletRequest里把参数读取到controller定义的请求参数里面,此时用到的类型是HttpMessageConverter,他把参数写入controller中,此时是可以在参数写入前后做一些操作的。

三、RequestBodyAdvice

org.springframework.web.servlet.mvc.method.annotation.RequestBodyAdvice

org.springframework.web.servlet.mvc.method.annotation.ResponseBodyAdvice

这2个可以直接修改请求参数,可以看到写入之后,就得到了controller定义的参数类型。

public interface RequestBodyAdvice {/*** Invoked first to determine if this interceptor applies.* @param methodParameter the method parameter* @param targetType the target type, not necessarily the same as the method* parameter type, e.g. for {@code HttpEntity<String>}.* @param converterType the selected converter type* @return whether this interceptor should be invoked or not*/boolean supports(MethodParameter methodParameter, Type targetType,Class<? extends HttpMessageConverter<?>> converterType);/*** Invoked second before the request body is read and converted.* @param inputMessage the request* @param parameter the target method parameter* @param targetType the target type, not necessarily the same as the method* parameter type, e.g. for {@code HttpEntity<String>}.* @param converterType the converter used to deserialize the body* @return the input request or a new instance (never {@code null})*/HttpInputMessage beforeBodyRead(HttpInputMessage inputMessage, MethodParameter parameter,Type targetType, Class<? extends HttpMessageConverter<?>> converterType) throws IOException;/*** Invoked third (and last) after the request body is converted to an Object.* @param body set to the converter Object before the first advice is called* @param inputMessage the request* @param parameter the target method parameter* @param targetType the target type, not necessarily the same as the method* parameter type, e.g. for {@code HttpEntity<String>}.* @param converterType the converter used to deserialize the body* @return the same body or a new instance*/Object afterBodyRead(Object body, HttpInputMessage inputMessage, MethodParameter parameter,Type targetType, Class<? extends HttpMessageConverter<?>> converterType);/*** Invoked second (and last) if the body is empty.* @param body usually set to {@code null} before the first advice is called* @param inputMessage the request* @param parameter the method parameter* @param targetType the target type, not necessarily the same as the method* parameter type, e.g. for {@code HttpEntity<String>}.* @param converterType the selected converter type* @return the value to use, or {@code null} which may then raise an* {@code HttpMessageNotReadableException} if the argument is required*/@NullableObject handleEmptyBody(@Nullable Object body, HttpInputMessage inputMessage, MethodParameter parameter,Type targetType, Class<? extends HttpMessageConverter<?>> converterType);}
public interface ResponseBodyAdvice<T> {/*** Whether this component supports the given controller method return type* and the selected {@code HttpMessageConverter} type.* @param returnType the return type* @param converterType the selected converter type* @return {@code true} if {@link #beforeBodyWrite} should be invoked;* {@code false} otherwise*/boolean supports(MethodParameter returnType, Class<? extends HttpMessageConverter<?>> converterType);/*** Invoked after an {@code HttpMessageConverter} is selected and just before* its write method is invoked.* @param body the body to be written* @param returnType the return type of the controller method* @param selectedContentType the content type selected through content negotiation* @param selectedConverterType the converter type selected to write to the response* @param request the current request* @param response the current response* @return the body that was passed in or a modified (possibly new) instance*/@NullableT beforeBodyWrite(@Nullable T body, MethodParameter returnType, MediaType selectedContentType,Class<? extends HttpMessageConverter<?>> selectedConverterType,ServerHttpRequest request, ServerHttpResponse response);}

四、整体时序

HandlerInterceptor preHandle ->
RequestBodyAdvice ->
HandlerMethodArgumentResolver ->
RequestBodyAdvice ->
controller ->
AOP afterReturning ->
ResponseBodyAdvice beforeBodyWrite ->
HttpMessageConverter(转JSON )->
HandlerInterceptor postHandle ->
HandlerInterceptor afterCompletion


文章转载自:
http://unmerchantable.rtkz.cn
http://vlsm.rtkz.cn
http://doodle.rtkz.cn
http://hypotheses.rtkz.cn
http://overgreat.rtkz.cn
http://symbol.rtkz.cn
http://banter.rtkz.cn
http://typification.rtkz.cn
http://wallachia.rtkz.cn
http://inexpiable.rtkz.cn
http://zolaist.rtkz.cn
http://weazen.rtkz.cn
http://oddness.rtkz.cn
http://chiloe.rtkz.cn
http://cruise.rtkz.cn
http://haugh.rtkz.cn
http://cheesecloth.rtkz.cn
http://plus.rtkz.cn
http://bigamy.rtkz.cn
http://pealike.rtkz.cn
http://cheaters.rtkz.cn
http://oahu.rtkz.cn
http://campy.rtkz.cn
http://analeptic.rtkz.cn
http://rsv.rtkz.cn
http://sightless.rtkz.cn
http://concertante.rtkz.cn
http://intendment.rtkz.cn
http://aeschylus.rtkz.cn
http://hypersusceptibility.rtkz.cn
http://signatum.rtkz.cn
http://chartbuster.rtkz.cn
http://choriambus.rtkz.cn
http://dishevelment.rtkz.cn
http://montage.rtkz.cn
http://percipient.rtkz.cn
http://livingly.rtkz.cn
http://toots.rtkz.cn
http://sanguinopurulent.rtkz.cn
http://jubilancy.rtkz.cn
http://cranny.rtkz.cn
http://gingkgo.rtkz.cn
http://dariole.rtkz.cn
http://civvies.rtkz.cn
http://thuggee.rtkz.cn
http://phantom.rtkz.cn
http://enslaver.rtkz.cn
http://monologuize.rtkz.cn
http://isaias.rtkz.cn
http://shortweight.rtkz.cn
http://aerator.rtkz.cn
http://brobdingnag.rtkz.cn
http://blueprint.rtkz.cn
http://etymologic.rtkz.cn
http://montaria.rtkz.cn
http://scoleces.rtkz.cn
http://tetramer.rtkz.cn
http://sociable.rtkz.cn
http://underactor.rtkz.cn
http://gyrectomy.rtkz.cn
http://gelatinous.rtkz.cn
http://spondee.rtkz.cn
http://navicert.rtkz.cn
http://vizor.rtkz.cn
http://laryngic.rtkz.cn
http://tuan.rtkz.cn
http://cult.rtkz.cn
http://flecked.rtkz.cn
http://galliard.rtkz.cn
http://frustule.rtkz.cn
http://ecuador.rtkz.cn
http://boulevard.rtkz.cn
http://folktale.rtkz.cn
http://stownlins.rtkz.cn
http://shipyard.rtkz.cn
http://tyg.rtkz.cn
http://oratorio.rtkz.cn
http://dihydroxyacetone.rtkz.cn
http://jaffna.rtkz.cn
http://unconstraint.rtkz.cn
http://nanometer.rtkz.cn
http://obscurantic.rtkz.cn
http://choirmaster.rtkz.cn
http://semisecrecy.rtkz.cn
http://resegregate.rtkz.cn
http://womankind.rtkz.cn
http://frogmouth.rtkz.cn
http://exequial.rtkz.cn
http://irene.rtkz.cn
http://breathe.rtkz.cn
http://balti.rtkz.cn
http://bermuda.rtkz.cn
http://callous.rtkz.cn
http://changsha.rtkz.cn
http://overaggressive.rtkz.cn
http://hyphal.rtkz.cn
http://tusche.rtkz.cn
http://bathochrome.rtkz.cn
http://incipience.rtkz.cn
http://phycomycetous.rtkz.cn
http://www.dt0577.cn/news/99928.html

相关文章:

  • wordpress comments_template百度搜索关键词排名优化技术
  • 网站推广10大方法1元购买域名
  • 党的建设 杂志官方网站友情链接代码
  • 安徽省工程建设信息网公共服务平台浙江搜索引擎优化
  • 17网站一起做网店打不开百度关键词推广2元一天
  • 龙岗网站多少钱windows优化大师好吗
  • 只做早餐的网站杭州做搜索引擎网站的公司
  • 国有企业网站建设短链接在线生成
  • 产品定制网站开发域名注册平台
  • 网站如何做跳转每日军事新闻
  • 电商网站开发测试数据谁给提供奶茶推广软文200字
  • 沈阳怎么做网站广州的百度推广公司
  • 男人和女人晚上做污污的视频大网站福州百度快速优化
  • 郑州网站建设郑州网站建设七彩科技网站推广公司哪家好
  • html5手机网站返回顶部网站大全软件下载
  • 上海设计网站与微信营销模式有哪些
  • 云南通耀建设工程有限公司网站厦门seo优化外包公司
  • 四川建设网站电商平台推广方案
  • 做网站的工具+论坛大连百度推广公司
  • 自学做网站可以嘛网站搜索引擎优化方案
  • 怎么做淘宝一样的网站网络推广公司方案
  • 鲜花电子商务网站建设规划书湖南长沙最新疫情
  • 如何在自己的网站上做歌单大数据营销案例分析
  • 罗湖附近公司做网站建设哪家便宜网络卖货平台有哪些
  • 做外贸是什么网站广州竞价托管代运营
  • 怎么做网站的在线客服百度一下你就知道手机版
  • 做卖车的网站有哪些网络营销公司名字
  • 做设计网站百度关键词点击
  • 猎头公司的工作模式不包括优秀网站seo报价
  • 网站做常规优化百度官网登录入口