【问题标题】:Spring mock mvc keep file extensionSpring mock mvc 保持文件扩展名
【发布时间】:2013-11-11 22:59:25
【问题描述】:

我能在这里找到的唯一相关问题是this one。

不幸的是,与上述问题不同,我没有使用文件名作为路径变量。我在一个字符串中发送的内容可能有也可能没有'.'。因此,我不想使用上述问题的解决方案中提到的策略。

这是我的 WebConfigAdapter:

@Configuration
public class WebConfigAdapter extends WebMvcConfigurerAdapter {
    @Autowired
    HandlerExceptionResolver exceptionHandler;

    /**
     * Disable content negotiation for path extensions.
     * 
     * @param configurer
     */
    @Override
    public void configureContentNegotiation(ContentNegotiationConfigurer configurer) {
            configurer.favorPathExtension(false);
    }

    /**
     * Set the exception handler.
     * 
     * @param exceptionResolvers
     */
    @Override
    public void configureHandlerExceptionResolvers(List<HandlerExceptionResolver> exceptionResolvers) {
            super.configureHandlerExceptionResolvers(exceptionResolvers);
            exceptionResolvers.add(exceptionHandler);
    }

    /**
     * Serve static resources.
     * 
     * @param registry
     */
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
            registry.addResourceHandler("/resources/**").addResourceLocations("/resources/");
    }
}

我在我的 WebConfig(一个 java 文件)中导入 WebConfigAdapater,如下所示:

@Configuration
@EnableWebMvc
@Import(value = { WebConfigAdapter.class, WebConfigSupport.class })
public class WebConfig {
    @Bean
    public InternalResourceViewResolver internalResourceViewResolver() {
            InternalResourceViewResolver resolver = new InternalResourceViewResolver();
            resolver.setPrefix("/WEB-INF/views/");
            resolver.setSuffix(".jsp");
            return resolver;
    }

    @Bean
    public HandlerExceptionResolver exceptionHandler() {
            return new ExceptionHandler();
    }
}

在我的应用程序上下文 xml 中:

<bean class="com.quova.platform.portal.config.WebConfig"/>

我仍然无法弄清楚当我在单元测试中使用 Spring mock mvc 时,为什么下面的控制器会收到字符串输入,它会在字符串中的最后一个句点(包括句点)之后去除字符。

 @RequestMapping(value = "/{ip}", method = RequestMethod.GET, produces = "application/json")
 @ResponseBody
 public Info lookup(@PathVariable("ip") String ip) throws Exception {

这是我在单元测试中调用上述端点的方式:

String ip = "4.3.2";
mockMvc.perform(get("/secure/resources/iplookup/{ip}", ip)).andExpect(status().isBadRequest());

所以现在控制器看到的是“4.3”。这也是我关于堆栈溢出的第一个问题,所以如果我遗漏了一些需要的信息或者可以更好地解释我的问题,请告诉我!感谢您的帮助!

【问题讨论】:

  • @SotiriosDelimanolis 我也有一个 WebMvcConfigurationSupport 与链接中提到的相同。
  • 您是否尝试了两个答案的建议?
  • 虽然使用 value = "/{ip:.*}" 有效。谢谢!
  • 我很好奇的是,当我将路径扩展名设置为 false 并将后缀模式匹配设置为 false 时,为什么它在不更改值的情况下无法正常工作。

标签: java unit-testing spring-mvc spring-mvc-test


【解决方案1】:
@RequestMapping(value = "/{ip:.*}", method = RequestMethod.GET, produces = "application/json")
 @ResponseBody
 public Info lookup(@PathVariable("ip") String ip) throws Exception {

这行得通

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-11-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多