0

使用严格的基于注释/仅 Java 的 Spring MVC 3.2.2 配置,我试图创建一个带有自定义类(我将调用它Context)作为参数的控制器方法。我想Context根据当前的知识进行构建,HttpServletRequest然后传递给控制器​​方法。本质上,我想在将请求对象发送到控制器之前创建自己的自定义包装器。例如我想做到这一点:

@Controller
@RequestMapping(value = "/")
public class MainController {

    @RequestMapping(value = "/")
    public @ResponseBody
    String process(HttpServletRequest request) {
        Context context = new Context(request);
        ...
    }

}

自动像这样:

@Controller
@RequestMapping(value = "/")
public class MainController {

    @RequestMapping(value = "/")
    public @ResponseBody
    String process(Context context) {
        ...
    }

}

这可能吗?我考虑在 my 中实现 a HandlerMethodArgumentResolveras a ,但我认为这不是正确的途径。我也尝试添加to (as a ) 也无济于事。我想有或可能在一个?@BeanWebMvcConfigurerAdapter@AutoWiredContext@BeanWebMvcConfigurerAdapterAbstractAnnotationConfigDispatcherServletInitializer

4

1 回答 1

0

HandlerMethodArgumentResolver and WebMvcConfigurerAdapter is certainly a right way to achieve your goal.

In order to register custom argument resolver you need to make your @Configuration extend WebMvcConfigurerAdapter and override its addArgumentResolver() method.

于 2013-10-29T17:37:47.543 回答