RequestMappingHandlerMapping
RequestMappingHandlerMapping
将http请求映射到处理的方法上,负责解析处理器方法上的注解…如:@RequestMapping
,@GetMapping
,,@PostMapping
,将请求路径,请求方法,请求参数等信息和 处理器方法建立映射
WebMvcRegistrations接口
WebMvcRegistration
: 允许自定义注册以下的spring mvc 组件:
- RequestMappingHandlerMapping : 处理器映射器,,处理器方法的查找
- RequestMappingHandlerAdapter : 处理器适配器,负责处理器方法的调用,参数绑定,和返回值处理
- HandlerExceptionResolver : 处理 处理器方法抛出的异常
- ViewResolver : 视图解析器
实现这个WebMvcRegistrations
可以定义自己的 ,处理器映射器,等,,,
自动拼接目录作为请求的前缀
- 写一个自己的RequestMappingHandlerMapping
@Component
public class AutoPrefixUrlMapping extends RequestMappingHandlerMapping {@Value("${missyou.api-package}")private String apiPackage;/**** @param method 请求要处理的方法* @param handlerType 控制器类,指要处理该请求的处理器类*/@Overrideprotected RequestMappingInfo getMappingForMethod(Method method, Class<?> handlerType) {RequestMappingInfo requestMappingInfo = super.getMappingForMethod(method, handlerType);if (requestMappingInfo != null){String prefix = this.getPrefix(handlerType);RequestMappingInfo newRequestMappingInfo = RequestMappingInfo.paths(prefix).build().combine(requestMappingInfo);return newRequestMappingInfo;}return requestMappingInfo;}private String getPrefix(Class<?> handlerType) {String path = handlerType.getPackage().getName();String dotpath = path.replace(apiPackage,"");return dotpath.replace(".","/");}}
- 让springboot使用这个RequestMappingHandlerMapping
实现WebMvcRegistration
注册自己的 RequestMappingHandlerMapping
@Component
public class AutoPrefixConfiguration implements WebMvcRegistrations {@Overridepublic RequestMappingHandlerMapping getRequestMappingHandlerMapping() {return new AutoPrefixUrlMapping();}
}