java - Spring模拟mvc保留文件扩展名

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

我在这里能找到的唯一相关问题是 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 文件)中导入 WebConfigAdapter,如下所示:

@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 模拟 mvc 时,为什么下面的 Controller 接收字符串输入,它会删除字符串中最后一个句点(包括句点)之后的字符。

 @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());

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

最佳答案

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

这有效

关于java - Spring模拟mvc保留文件扩展名,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19714972/

相关文章:

java - 捕获字符串中的属性值?

java - 结合 MethodHandles.publicLookup() 与 Method.setAccessible(true)

unit-testing - 如何在 BDD/TDD 中表达 Action 的结果

python - 如何使用 Unittest 套件传递变量

java - 当 RequestMapping 中的最后一个 PathVariable 只有

java - Scala/Lift 与 Java/Spring。为什么要打架?

java - 将 boolean 参数设置为另一个 boolean 值的相反值?

java - Mahout 在行动 : Chapter 06: Wikipedia job fails with java. lang.ArrayIndexOutOfBoundsException

angular - 使用 tick 和 fakeAsync 对 Observable 进行单元测试 Angular 组件

java - Spring REST Hibernate 应用设计