重庆有哪些做网站的公司,河南做网站优化,龙海网络推广,网站建设要写代码吗RequestMappingHandlerMapping
RequestMappingHandlerMapping将http请求映射到处理的方法上,负责解析处理器方法上的注解…如#xff1a;RequestMapping ,GetMapping#xff0c;#xff0c;PostMapping,将请求路径#xff0c;请求方法#xff0c;请求参数等信息和 处理器…RequestMappingHandlerMapping
RequestMappingHandlerMapping将http请求映射到处理的方法上,负责解析处理器方法上的注解…如RequestMapping ,GetMappingPostMapping,将请求路径请求方法请求参数等信息和 处理器方法建立映射
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();}
}