当前位置:网站首页>Application and principle of handlermethodargumentresolver
Application and principle of handlermethodargumentresolver
2020-11-07 17:19:00 【LBJ Yu】
HandlerMethodArgumentResolver Policy interface for parsing method parameters to parameter values , What we often call a custom parameter parser , Source code is as follows
/**
* Strategy interface for resolving method parameters into argument values in
* the context of a given request.
*
* @author Arjen Poutsma
* @since 3.1
* @see HandlerMethodReturnValueHandler
*/
public interface HandlerMethodArgumentResolver {
/**
* Whether the given {@linkplain MethodParameter method parameter} is
* supported by this resolver.
* @param parameter the method parameter to check
* @return {@code true} if this resolver supports the supplied parameter;
* {@code false} otherwise
*/
boolean supportsParameter(MethodParameter parameter);
/**
* Resolves a method parameter into an argument value from a given request.
* A {@link ModelAndViewContainer} provides access to the model for the
* request. A {@link WebDataBinderFactory} provides a way to create
* a {@link WebDataBinder} instance when needed for data binding and
* type conversion purposes.
* @param parameter the method parameter to resolve. This parameter must
* have previously been passed to {@link #supportsParameter} which must
* have returned {@code true}.
* @param mavContainer the ModelAndViewContainer for the current request
* @param webRequest the current request
* @param binderFactory a factory for creating {@link WebDataBinder} instances
* @return the resolved argument value, or {@code null} if not resolvable
* @throws Exception in case of errors with the preparation of argument values
*/
@Nullable
Object resolveArgument(MethodParameter parameter, @Nullable ModelAndViewContainer mavContainer,
NativeWebRequest webRequest, @Nullable WebDataBinderFactory binderFactory) throws Exception;
}
Here's my little example , It is mainly through the custom parameter parser to change the paging parameters , Extract them and encapsulate them into entity objects
1. First, customize an annotation
/**
* @Author: Yu Yunbo (18088704)
* @Date: 2020/11/7 10:18
* @Description:
*/
@Target(ElementType.PARAMETER)
@Inherited
@Documented
@Retention(RetentionPolicy.RUNTIME)
public @interface CustomArgumentPage {
}
2. Write a custom parameter parser implementation class
import com.suning.logistics.jwms.on.base.vo.PageBean;
import org.apache.commons.lang3.StringUtils;
import org.springframework.core.MethodParameter;
import org.springframework.web.bind.support.WebDataBinderFactory;
import org.springframework.web.context.request.NativeWebRequest;
import org.springframework.web.method.support.HandlerMethodArgumentResolver;
import org.springframework.web.method.support.ModelAndViewContainer;
import javax.servlet.http.HttpServletRequest;
/**
* @Author: Yu Yunbo (18088704)
* @Date: 2020/11/7 10:14
* @Description:
*/
public class CustomArgumentResolvers implements HandlerMethodArgumentResolver {
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.hasParameterAnnotation(CustomArgumentPage.class);
}
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer mavContainer, NativeWebRequest webRequest, WebDataBinderFactory binderFactory) throws Exception {
HttpServletRequest request = webRequest.getNativeRequest(HttpServletRequest.class);
String page = request.getParameter("page");
String pageSize = request.getParameter("pageSize");
PageBean pageBean = new PageBean();
if (StringUtils.isNotEmpty(page)) {
pageBean.setCurrentPage(Integer.valueOf(page).intValue());
}
if (StringUtils.isNotEmpty(pageSize)) {
pageBean.setPageSize(Integer.valueOf(pageSize).intValue());
}
pageBean.setStartPage((pageBean.getCurrentPage()-1)*pageBean.getPageSize());
return pageBean;
}
}
3. Inject a custom parser into IOC in
@Bean
public CustomArgumentResolvers initCustomArgumentResolvers(){
return new CustomArgumentResolvers();
}
4. Will custom parameter parser , Injection into MvcConfigure in
@Configuration
public class WebMvcCustomConfigurer implements WebMvcConfigurer {
@Autowired
private CustomArgumentResolvers customArgumentResolvers;
/**
* Add resolvers to support custom controller method argument types.
* <p>This does not override the built-in support for resolving handler
* method arguments. To customize the built-in support for argument
* resolution, configure {@link RequestMappingHandlerAdapter} directly.
*
* @param resolvers initially an empty list
*/
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> resolvers) {
resolvers.add(customArgumentResolvers);
}
}
5. Write tests Controller
@RequestMapping("/queryPageConfigList")
@ResponseBody
public Object getWarehouseCodeReadyonly(@CustomArgumentPage PageBean pageBean){
return warehouseConfigService.queryPage(pageBean,null);
}
6. The effect is as follows

The following source code analysis of a wave , Tell me about the whole process ,
1. stay Springboot AutoConfiguration Will automatically inject WebMvcAutoConfiguration


2. stay WebMvcAutoConfiguration in Static inner class EnableWebMvcConfiguration It inherited DelegatingWebMvcConfiguration This class , This is very important , Medium requestMappingHandlerAdapter Method , initialization HandlerAdapter
super.requestMappingHandlerAdapter()

3. In the parent class WebMvcConfigurationSupport in , Here's the picture 565 Line code , Will give HandlerAdapter Set custom parameter parser

4. Get into getArgumentResolvers Method , The core approach addArgumentResolvers


5. It's an empty method ???, Source tracking is into its subclass DelegatingWebMvcConfiguration In this class

6. Continue to code trace the next step , Will arrive WebMvcConfigurerComposite in addArgumentResolvers Method , Cycle WebMvcConfigurer List Call the addArgumentResolvers As for this this.delegates How to initialize , You can study for yourself

7. Finally, it will be transferred to our customized WebMvcCustomConfigurer in , Will our custom parameter parser , Add to the entire parameter resolution List in

8. Analyze the number parameter List Set to HandlerAdapter in

9. When called , The whole call chain is shown in the figure below , Just interested students can analyze it by themselves

版权声明
本文为[LBJ Yu]所创,转载请带上原文链接,感谢
边栏推荐
- Application of UHF RFID medical blood management system
- Nonvolatile MRAM memory used in all levels of cache
- Analysis of the original code of [QT] qthread
- 10000! Ideal car recalls all defective cars: 97 accidents have occurred and losses will be expanded
- .NETCore3.1+Vue.js打造的低代码工作流引擎
- 嘉宾介绍|2020 PostgreSQL亚洲大会中文分论坛:潘娟
- HMS Core推送服务,助力电商App开展精细化运营
- 如何才能快速正确的部署甘特图
- 如何利用PopupWindow实现弹出菜单并解决焦点获取以及与软键盘冲突问题
- Windows 10 Bluetooth management page 'add Bluetooth or other devices' option click no response solution
猜你喜欢

k-vim安装及The ycmd server SHUT DOWN (restart with ':YcmRestartServer')这种错误的解决方法

Gantt chart grouping activities tutorial

How does varhart xgantt represent working days on a calendar

Plug in bilibilibili new version 0.5.5

使用LWA和Lync模拟外部测试无边缘单前端环境

嘉宾介绍|2020 PostgreSQL亚洲大会中文分论坛:潘娟

Classroom exercises

K-vim installation and the ycmd server shut down (restart with ': ycmrestartserver')

Test the necessary skill points of siege lion! This article takes you to interpret the testing technology under Devops

图像处理工具包ImagXpress使用教程,如何查看事件
随机推荐
confd
【笔记】Error while loading PyV8 binary: exit code 1解决方法
图像处理工具包ImagXpress使用教程,如何查看事件
Stm32f030f4p6 compatible with smart micro mm32f031f4p6
条形码识别器Dynamsoft Barcode Reader v7.5全新上线!
How to deploy Gantt chart quickly and correctly
Microservice - how to limit and fuse service current
Gantt chart grouping activities tutorial
Talk about sharing before paying
Windows 10 Bluetooth management page 'add Bluetooth or other devices' option click no response solution
【涂鸦物联网足迹】物联网主流通信方式
Introduction to Jenkins (2) declarative pipeline
Benefits and functions of auto maintenance app development
10000! Ideal car recalls all defective cars: 97 accidents have occurred and losses will be expanded
HMS core push service helps e-commerce app to carry out refined operation
Stm32f030k6t6 compatible replacement smart mm32f031k6t6
谈了多年的数字化转型,为什么还有很多企业依然“口头管理”
Do you really know how to use search engines?
idea 激活到 2089 失效
Using LWA and lync to simulate external test edge free single front end environment