MockMvc WebAppConfiguration:在web.xml中加载servlet映射 [英] MockMvc WebAppConfiguration: Load servlet mappings in web.xml

查看:231
本文介绍了MockMvc WebAppConfiguration:在web.xml中加载servlet映射的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用MockMvc编写集成测试,我想知道是否存在一种从web.xml加载servlet映射的方法(通常不重要).

I'm writing integration tests using MockMvc, and I'm wondering if there's a way to load servlet mappings from web.xml (which normally shouldn't matter).

我有一个自定义HandlerInteceptor,该自定义HandlerInteceptor将请求URI(来自HttpServletRequest)与模板(使用AntPathMatcher)进行匹配.

I have a custom HandlerInteceptor that matches the request URI (from HttpServletRequest) against a template (using AntPathMatcher).

在web.xml中,我定义了这样的servlet映射(以及相应的mobile-context.xml):

In web.xml, I define servlet mappings like this (with a corresponding mobile-context.xml):

<servlet-mapping>
    <servlet-name>mobileServlet</servlet-name>
    <url-pattern>/services/*</url-pattern>
</servlet-mapping>

因此,当控制器定义像"/operation"这样的映射时,实际上应该向"/services/operation"发出请求.我的自定义HandlerInterceptor将URI请求与"/**/services/{operationName}/**"之类的模板进行匹配.

So when a controller defines a mapping like "/operation", requests should really be made to "/services/operation". My custom HandlerInterceptor matches URI requests against a template like "/**/services/{operationName}/**".

我的应用程序在Tomcat上运行良好.但是,在@ContextConfiguration中,由于web.xml不是spring配置文件,所以只能指定mobile-context.xml.因此,MockMvc仅允许我向"/operation"而不是"/services/operation"发出请求,从而导致我的HandlerInterceptor引发异常.

My application works perfectly on Tomcat. However, in @ContextConfiguration, I can only specify mobile-context.xml since web.xml is not a spring config file. Thus, MockMvc only allows me to make requests to "/operation" and not "/services/operation", causing my HandlerInterceptor to throw an exception.

是否有任何方法可以让MockMvc注册servlet映射,或者有任何巧妙的方法来解决此问题?预先感谢.

Is there any way to get MockMvc to register the servlet mappings, or any clever ways around this? Thanks in advance.

一个

A similar question here suggests what I need is not possible, but I don't have permission to change the source code, so I can't modify the template or the HandlerInterceptor.

推荐答案

无法加载web.xml映射.但是,您可以在请求中显式设置上下文路径,servlet路径和路径信息.有关MockHttpServletRequestBuilder中这些方法的信息,请参见Javadoc.

There is no way to load the web.xml mappings. However you can explicitly set the context path, servlet path, and path info on the request. See the Javadoc for those methods in MockHttpServletRequestBuilder.

这篇关于MockMvc WebAppConfiguration:在web.xml中加载servlet映射的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆