In addition to Weibo, there is also WeChat
Please pay attention
WeChat public account
Shulou
2025-01-16 Update From: SLTechnology News&Howtos shulou NAV: SLTechnology News&Howtos > Internet Technology >
Share
Shulou(Shulou.com)06/02 Report--
What is the role of HandlerMethodArgumentResolver in Springboot, many novices are not very clear about this, in order to help you solve this problem, the following editor will explain for you in detail, people with this need can come to learn, I hope you can gain something.
RequestMappingHandlerAdapter
Figure 1
HandlerMethodArgumentResolver is an interface, and there are only two methods. The true mark returned by supportsParameter supports parsing, and then call resolveArgument to pass the result as a parameter, as shown in List-1 below.
List-1
Public interface HandlerMethodArgumentResolver {boolean supportsParameter (MethodParameter var1); @ Nullable Object resolveArgument (MethodParameter var1, @ Nullable ModelAndViewContainer var2, NativeWebRequest var3, @ Nullable WebDataBinderFactory var4) throws Exception;}
There is an attribute argumentResolvers in RequestMappingHandlerAdapter, which is HandlerMethodArgumentResolverComposite, and you can see from the class name that the composition pattern is used. RequestMappingHandlerAdapter's getDefaultArgumentResolvers, these are the default parameter handlers.
List-2
Private List getDefaultArgumentResolvers () {List resolvers = new ArrayList (); / / Annotation-based argument resolution resolvers.add (new RequestParamMethodArgumentResolver (getBeanFactory (), false)); resolvers.add (new RequestParamMapMethodArgumentResolver ()); resolvers.add (new PathVariableMethodArgumentResolver ()); resolvers.add (new PathVariableMapMethodArgumentResolver ()); resolvers.add (new MatrixVariableMethodArgumentResolver ()); resolvers.add (new MatrixVariableMapMethodArgumentResolver ()); resolvers.add (new ServletModelAttributeMethodProcessor (false)) Resolvers.add (new RequestResponseBodyMethodProcessor (getMessageConverters (), this.requestResponseBodyAdvice)); resolvers.add (new RequestPartMethodArgumentResolver (getMessageConverters (), this.requestResponseBodyAdvice)); resolvers.add (new RequestHeaderMethodArgumentResolver (getBeanFactory (); resolvers.add (new RequestHeaderMapMethodArgumentResolver ()); resolvers.add (new ServletCookieValueMethodArgumentResolver (getBeanFactory (); resolvers.add (new ExpressionValueMethodArgumentResolver (getBeanFactory (); resolvers.add (new SessionAttributeMethodArgumentResolver ()); resolvers.add (new RequestAttributeMethodArgumentResolver ()) / / Type-based argument resolution resolvers.add (new ServletRequestMethodArgumentResolver ()); resolvers.add (new ServletResponseMethodArgumentResolver ()); resolvers.add (new HttpEntityMethodProcessor (getMessageConverters (), this.requestResponseBodyAdvice)); resolvers.add (new RedirectAttributesMethodArgumentResolver ()); resolvers.add (new ModelMethodProcessor ()); resolvers.add (new MapMethodProcessor ()); resolvers.add (new ErrorsMethodArgumentResolver ()); resolvers.add (new SessionStatusMethodArgumentResolver ()); resolvers.add (new UriComponentsBuilderMethodArgumentResolver ()) / / Custom arguments if (getCustomArgumentResolvers ()! = null) {resolvers.addAll (getCustomArgumentResolvers ());} / / Catch-all resolvers.add (new RequestParamMethodArgumentResolver (getBeanFactory (), true)); resolvers.add (new ServletModelAttributeMethodProcessor (true)); return resolvers;}
This is shown in figure 2 below:
Figure 2
In the doDispatch method of DispatcherServlet, the invokeAndHandle method of ServletInvocableHandlerMethod is called indirectly, followed by a call to HandlerMethodArgumentResolver one by one. So our custom HandlerMethodArgumentResolver is handled at steps 9 and 10 in figure 2. As shown in List-3 below, iterate through all the parameters, call HandlerMethodArgumentResolverComposite's support and resolve methods for each parameter, and HandlerMethodArgumentResolverComposite delegates to each HandlerMethodArgumentResolver for processing.
List-3
Private Object [] getMethodArgumentValues (NativeWebRequest request, ModelAndViewContainer mavContainer, Object... ProvidedArgs) throws Exception {MethodParameter [] parameters = getMethodParameters (); Object [] args = new Object [parameters.length]; for (int I = 0; I < parameters.length; iTunes +) {MethodParameter parameter = parameters [I]; parameter.initParameterNameDiscovery (this.parameterNameDiscoverer); args [I] = resolveProvidedArgument (parameter, providedArgs); if (args [I]! = null) {continue } if (this.argumentResolvers.supportsParameter (parameter)) {try {args [I] = this.argumentResolvers.resolveArgument (parameter, mavContainer, request, this.dataBinderFactory); continue } catch (Exception ex) {if (logger.isDebugEnabled ()) {logger.debug (getArgumentResolutionErrorMessage ("Failed to resolve", I), ex);} throw ex }} if (args [I] = = null) {throw new IllegalStateException ("Could not resolve method parameter at index" + parameter.getParameterIndex () + "in" + parameter.getMethod () .toGenericString () + ":" + getArgumentResolutionErrorMessage ("No suitable resolver for", I));}} return args;}
It is worth noting that step 12 in figure 2, HandlerMethodReturnValueHandler, can change the value returned by controller. If step 7 throws an exception, the HandlerMethodReturnValueHandler step will not be executed, so HandlerMethodReturnValueHandler cannot handle situations where business code throws an exception.
Is it helpful for you to read the above content? If you want to know more about the relevant knowledge or read more related articles, please follow the industry information channel, thank you for your support.
Welcome to subscribe "Shulou Technology Information " to get latest news, interesting things and hot topics in the IT industry, and controls the hottest and latest Internet news, technology news and IT industry trends.
Views: 0
*The comments in the above article only represent the author's personal views and do not represent the views and positions of this website. If you have more insights, please feel free to contribute and share.
Continue with the installation of the previous hadoop.First, install zookooper1. Decompress zookoope
"Every 5-10 years, there's a rare product, a really special, very unusual product that's the most un
© 2024 shulou.com SLNews company. All rights reserved.