Spring 处理程序映射的显式注册
我阅读了spring文档中更具体的处理程序映射部分,并了解到我们可以在运行时注册处理程序映射方法。我理解它是如何实现的,但我无法理解的是,为什么我们首先需要这样的功能 请参阅以下代码片段以注册处理程序Spring 处理程序映射的显式注册,spring,spring-mvc,handler,Spring,Spring Mvc,Handler,我阅读了spring文档中更具体的处理程序映射部分,并了解到我们可以在运行时注册处理程序映射方法。我理解它是如何实现的,但我无法理解的是,为什么我们首先需要这样的功能 请参阅以下代码片段以注册处理程序 @Autowired public void setHandlerMapping(RequestMappingHandlerMapping mapping, HelloRestController handler) throws NoSuchMethodException {
@Autowired
public void setHandlerMapping(RequestMappingHandlerMapping mapping, HelloRestController handler)
throws NoSuchMethodException {
RequestMappingInfo info = RequestMappingInfo
.paths("/mycustomapi").methods(RequestMethod.GET).build();
Method method = HelloRestController.class.getMethod("customHandler");
mapping.registerMapping(info, handler, method);
}
如果有人能在需要的地方解释一些用例,那将很有帮助。谢谢。我从未使用过这个,但我可以想到一个实用程序: 由于注释仅接受常量表达式作为参数,因此不能编写类似的内容:
@GetMapping(requestMapping(…)//注释GetMapping.value的值必须是常量表达式
公共字符串customHandler(…){
...
}
但你可以这样写:
RequestMappingInfo=RequestMappingInfo
.路径(请求映射(…)
.methods(RequestMethod.GET)
.build();
换句话说,您可以为URI编写经过计算的处理程序(例如存储在配置文件中)。我从未使用过这个,但我可以想到一个实用程序: 由于注释仅接受常量表达式作为参数,因此不能编写类似的内容:
@GetMapping(requestMapping(…)//注释GetMapping.value的值必须是常量表达式
公共字符串customHandler(…){
...
}
但你可以这样写:
RequestMappingInfo=RequestMappingInfo
.路径(请求映射(…)
.methods(RequestMethod.GET)
.build();
换句话说,您可以为计算的URI编写处理程序(例如存储在配置文件中)