[英]Spring MockMvc trimming parameter
I have run into a problem that has me stumped. 我遇到了困扰我的问题。 I am running the latest version of Spring and JUnit. 我正在运行Spring和JUnit的最新版本。 I was experimenting with the MockMvc and Spring integration test classes in order to see if it would fit my companies needs and it was working well, until I tried to submit a request for that contained a @PathVariable which is a version number in the url. 我正在尝试使用MockMvc和Spring集成测试类,以查看它是否适合我的公司并且运行良好,直到尝试提交包含@PathVariable的请求为止,该请求是URL中的版本号。
Pom; 双响炮;
<modelVersion>4.0.0</modelVersion>
<parent>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-parent</artifactId>
<version>1.2.3.RELEASE</version>
</parent>
<groupId>asd</groupId>
<artifactId>asd</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>
<name>asd</name>
<url>http://maven.apache.org</url>
<properties>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<spring.version>4.1.6.RELEASE</spring.version>
</properties>
<dependencies>
<dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-web</artifactId>
<scope>compile</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit</artifactId>
<version>4.12</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>junit</groupId>
<artifactId>junit-dep</artifactId>
<version>4.5</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework</groupId>
<artifactId>spring-test</artifactId>
<version>${spring.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
Example Controller; 示例控制器; import junit.framework.Assert; 导入junit.framework.Assert;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;
@RestController
public class ExampleController {
@RequestMapping(value = "/example/appVersion/{applicationVersion}")
public @ResponseBody void example(@PathVariable("applicationVersion") String applicationVersion) {
if (!"1.0.0.0".equals(applicationVersion)) {
Assert.fail("I needed to be 1.0.0.0");
}
}
}
Test Class; 测试等级
import static org.springframework.test.web.servlet.request.MockMvcRequestBuilders.get;
import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.result.MockMvcResultMatchers.status;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;
import org.junit.Assert;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.test.WebIntegrationTest;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.web.servlet.MvcResult;
import org.springframework.web.context.WebApplicationContext;
@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations = "classpath*:appContext.xml")
@WebIntegrationTest
public class ExampleProblem {
@Autowired
WebApplicationContext context;
@Test
public void test() throws Exception {
String url = "/example/appVersion/1.0.0.0";
// String url = "/example/appVersion/{applicationVersion}";
//@formatter:off
MvcResult result = webAppContextSetup(context)
.build()
.perform(
get(url)
// get(url, "1.0.0.0")
.accept("application/json")
).andDo(print())
.andExpect(status().is(200))
.andReturn();
//@formatter:on
Assert.assertNotNull(result);
}
}
My appContext.xml file only contains this line (other than the initial beans tag and end tag); 我的appContext.xml文件仅包含此行(初始bean标签和结束标签除外);
<context:component-scan base-package="asd" />
When I view the request coming into my example controller, I can see with the debugger it says /example/appVersion/1.0.0.0 but the path variable is being set to "1.0.0" always. 当我查看进入示例控制器的请求时,可以在调试器中看到它说/example/appVersion/1.0.0.0,但路径变量始终设置为“ 1.0.0”。 It appears the last .0 is always being dropped because if I pass in "1.0.0.0.0" it trims to the expected "1.0.0.0". 似乎最后一个.0总是被删除,因为如果我输入“ 1.0.0.0.0”,它将修整为预期的“ 1.0.0.0”。
Any insight into what may be causing this or what I am doing wrong would be greatly appreciated. 对于可能导致此问题或我做错了什么的任何见解,将不胜感激。
Thank you. 谢谢。
This happens because Spring interprets the last dot (.) and following characters as a file extension. 发生这种情况是因为Spring将最后一个点(。)和其后的字符解释为文件扩展名。
In order to fix this, you need to tell Spring that the parameter expects dot characters by adding ":.+" to the end of the controller parameter. 为了解决这个问题,您需要通过在控制器参数的末尾添加“:。+”来告诉Spring该参数期望点字符。 So the annotation now looks like this; 这样,注释现在看起来像这样;
@RequestMapping(value = "/example/appVersion/{applicationVersion:.+}")
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.