当前位置:网站首页>Analysis and practice of parameter parser handlermethodargumentresolver
Analysis and practice of parameter parser handlermethodargumentresolver
2022-07-26 00:42:00 【likelong965】
Spring Parameter resolver –HandlerMethodArgumentResolver Analysis and actual combat
analysis
Since we need to analyze it , Take a look at this interface first
public interface HandlerMethodArgumentResolver {
boolean supportsParameter(MethodParameter var1);
@Nullable
Object resolveArgument(MethodParameter var1, @Nullable ModelAndViewContainer var2, NativeWebRequest var3, @Nullable WebDataBinderFactory var4) throws Exception;
}
There are two ways to interface supportsParameter and resolveArgument.
Method supportsParameter Well understood. , The return value is boolean type , Its function is to judge Controller Parameters in layer , Whether the conditions are met , If the conditions are met, execute resolveArgument Method , If you are not satisfied, skip .
and resolveArgument Methods? , It's only in supportsParameter Method returns true It will be called . Used to handle some business , Assign return value to Controller This parameter in the layer .
therefore , We can HandlerMethodArgumentResolver Understood as a parameter parser , We can implement... By writing a class HandlerMethodArgumentResolver Interface to implement Controller Initialization of method parameters in the layer .
actual combat
When the parameter parser is not used Controller controller :
@RestController
@RequestMapping("/build/project")
public class BuildSmProjectController extends BaseController {
@Autowired
private BuildSmProjectService buildSmProjectService;
@Autowired
private RedisUtils redis;
@GetMapping("list")
public R list(@RequestHeader("token") String token, BuildSmProject buildSmProject) {
startPage();
SysUser sysUser = redis.get(Constants.ACCESS_TOKEN + token, SysUser.class);
buildSmProject.setTenantId(sysUser.getTenantId());
return result(buildSmProjectService.selectBuildSmProjectList(buildSmProject));
}
}
list In the method , In order to obtain SysUser object , You need to get it in the user request header token, Then go to redis Get system user information . Just want to get user information , Will inevitably repeat the above code .
Next use HandlerMethodArgumentResolver Interface to perform a small optimization .
Use HandlerMethodArgumentResolver After the parser :
First make a custom annotation , Identification is used to parse user information
@Target(ElementType.PARAMETER)
@Retention(RetentionPolicy.RUNTIME)
public @interface LoginUser
{
}
Next, write a class implementation HandlerMethodArgumentResolver Interface
@Component
public class LoginUserHandlerResolver implements HandlerMethodArgumentResolver {
@Autowired
private RedisUtils redis;
// Judge whether the parameter is SysUser Class and whether there is @LoginUser annotation , return true perform resolveArgument Method
@Override
public boolean supportsParameter(MethodParameter parameter) {
return parameter.getParameterType().isAssignableFrom(SysUser.class)
&& parameter.hasParameterAnnotation(LoginUser.class);
}
// This method mainly obtains the request header parameters token, Get token Go to redis obtain SysUser The information is assigned to the above parameters
@Override
public Object resolveArgument(MethodParameter parameter, ModelAndViewContainer container, NativeWebRequest nativeWebRequest, WebDataBinderFactory factory) {
String token = ServletUtils.getHeader("token");
return redis.get(Constants.ACCESS_TOKEN + token, SysUser.class);
}
}
Put the class we wrote , Add to spring In the parser
@Configuration
public class WebMvcConfig implements WebMvcConfigurer
{
@Autowired
private LoginUserHandlerResolver loginUserHandlerResolver;
@Override
public void addArgumentResolvers(List<HandlerMethodArgumentResolver> argumentResolvers)
{
argumentResolvers.add(loginUserHandlerResolver);
}
}
So our control class can be written like this :
@RestController
@RequestMapping("/build/project")
public class BuildSmProjectController extends BaseController {
@Autowired
private BuildSmProjectService buildSmProjectService;
@GetMapping("list")
public R list(@LoginUser SysUser sysUser, BuildSmProject buildSmProject) {
startPage();
buildSmProject.setTenantId(sysUser.getTenantId());
return result(buildSmProjectService.selectBuildSmProjectList(buildSmProject));
}
}
Next, as long as the method wants to obtain user information, it only needs to request the current login user on the header token, write @LoginUser SysUser sysUser You can directly obtain the user information and assign it to sysUser, Reduce the writing of some redundant code .
边栏推荐
- 使用CMake编译OpenFoam求解器
- Preparation of bovine serum albumin modified by low molecular weight protamine lmwp/peg-1900 on the surface of albumin nanoparticles
- Data driven DDT for automated testing
- Wechat applet dynamic style | parameter transfer
- 【无标题】如何实现可插拔配置?
- Research on the influence of opinion leaders based on network analysis and text mining
- 8个小妙招-数据库性能优化,yyds~
- 2022/7/25 exam summary
- Preparation of bovine erythrocyte superoxide dismutase sod/ folic acid coupled 2-ME albumin nanoparticles modified by bovine serum albumin
- Seretod2022 track1 code analysis - task-based dialogue system challenge for semi supervised and reinforcement learning
猜你喜欢
随机推荐
Semaphore
P4047 [JSOI2010]部落划分
SQL time splicing problem, splicing recovery automatically truncated by the system
Hnoi2012 mine construction
8种MySQL常见SQL错误用法,我全中
DC-6 -- vulnhub range
[zero based BLDC series] brushless DC motor control principle based on the zero crossing detection method of back EMF
[oops framework] interface management
Data driven DDT for automated testing
Modeling and simulation analysis of online medical crowdfunding communication based on SEIR model
2022/7/25 exam summary
JMeter/IDEA中引用jar包json-path.jar的坎坷之路
【NumPy中数组创建】
2022/7/19 考试总结
2022/7/24 考试总结
Research on the integrated data quality management system and technical framework under the scenario of data circulation and transaction
开发还没联调,任务就要上线
以数据驱动管理转型,元年科技正当时
D3D计算着色器入门
【IJCAI 2022】参数高效的大模型稀疏训练方法,大幅减少稀疏训练所需资源

![[redis] ① introduction and installation of redis](/img/87/af98c862524a81d4636f1cb3be5181.png)






