Java 为什么不';t我的弹簧控制器';s请求映射在模拟WebApplicationContext中工作?

Java 为什么不';t我的弹簧控制器';s请求映射在模拟WebApplicationContext中工作?,java,spring,spring-mvc,spring-test,spring-test-mvc,Java,Spring,Spring Mvc,Spring Test,Spring Test Mvc,我正在使用Spring文档。我的目标是使用“webAppContextSetup”选项来测试我的spring配置和控制器,但是我在将控制器的方法映射到TestDispatcherServlet中时遇到了问题。到目前为止,我有以下设置: 数据配置 ContractControllerIT.java 目前,我在运行测试时看到以下错误: 此外,这是我关于StackOverflow的第一个问题,因此欢迎并感谢任何关于如何改进我的问题的建议。我看到了一些不同于我通常为控制器和测试所做的事情: Con

我正在使用Spring文档。我的目标是使用“webAppContextSetup”选项来测试我的spring配置和控制器,但是我在将控制器的方法映射到TestDispatcherServlet中时遇到了问题。到目前为止,我有以下设置:

数据配置

ContractControllerIT.java

目前,我在运行测试时看到以下错误:


此外,这是我关于StackOverflow的第一个问题,因此欢迎并感谢任何关于如何改进我的问题的建议。

我看到了一些不同于我通常为控制器和测试所做的事情:

ContractController.java-为两个请求映射添加斜杠:

@RequestMapping("/contract")
...
@RequestMapping("/{name}")
ContractControllerIT.java-添加斜杠:

@Test
public void getAccount() throws Exception {
    mockMvc.perform(get("/contract/{name}", "test")).andExpect(status().isOk());
}
最后一部分说明了一切:没有“契约/测试”的映射,这是您在集成测试中执行的
GET
请求

但是,控制器中的映射没有问题。唯一的问题是您在集成测试中省略了前导斜杠

以下解决了您的问题:

@测试
public void getAccount()引发异常{
perform(get(“/contract/{name}”,“test”))
.andExpect(status().isOk());
}
请特别注意
“/contract/{name}”
中的前导斜杠

问候,


Sam(SpringTestContext框架的作者)

修复了它。我很想知道你发现了什么让这变得不必要。考虑到spring文档没有提到这一点,这无疑是一个令人沮丧的问题。@jpack我很高兴它起了作用。当文档不正确或遗漏某些内容时,会令人沮丧。昨天我试着寻找为什么需要服务器,但没有找到任何答案。我今天打算删除这篇文章,因为我没有回应。方案和服务器名称不是必需的。虽然为了更清晰,建议在
@RequestMapping
路径中使用前导斜杠,但它们不是必需的。@SamBrannen嘿,谢谢!我在代码中也更改了它。克隆存储库并使用其示例运行的危害之一。仅供参考:
web.xml
与任何涉及
MockMvc
的讨论都完全无关,因为Spring MVC测试框架在Servlet容器之外运行。我想可能是这样的,但我的目的是展示我是如何分解配置文件的,以防出现问题。你认为我应该把它从问题中删除吗?是的,我会删除
web.xml
列表,因为它只会让人感到困惑。啊,我觉得问题可能是这样的愚蠢错误。谢谢你花时间写这么详尽的答案。
@RestController
@RequestMapping("contract")
public class ContractController {
    @Autowired
    DaoService daoService;

    @RequestMapping("{name}")
    public Contract getContract(@PathVariable String name) {
        return daoService.findContract(name);
    }
}
@RunWith(SpringJUnit4ClassRunner.class)
@WebAppConfiguration
@ContextConfiguration("/spring/*.xml")
public class ContractControllerIT {
    @Autowired
    private WebApplicationContext webApplicationContext;

    private MockMvc mockMvc;

    @Before
    public void setup() {
        mockMvc = MockMvcBuilders.webAppContextSetup(webApplicationContext).build();
    }

    @Test
    public void getAccount() throws Exception {
        mockMvc.perform(get("contract/{name}", "test")).andExpect(status().isOk());
    }

}
java.lang.AssertionError: Status expected:<200> but was:<404>
10:38:09.410 [main] INFO  org.springframework.beans.factory.xml.XmlBeanDefinitionReader - Loading XML bean definitions from class path resource [spring/mvc.xml]
10:38:09.469 [main] INFO  org.springframework.web.context.support.GenericWebApplicationContext - Refreshing org.springframework.web.context.support.GenericWebApplicationContext@7a4ccb53: startup date [Mon Sep 28 10:38:09 MDT 2015]; parent: org.springframework.web.context.support.GenericWebApplicationContext@4a22f9e2
10:38:09.607 [main] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping - Mapped "{[/contract/{name}]}" onto public com.example.test.domain.Contract com.example.test.controller.ContractController.getContract(java.lang.String) throws java.lang.Exception
10:38:09.786 [main] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@7a4ccb53: startup date [Mon Sep 28 10:38:09 MDT 2015]; parent: org.springframework.web.context.support.GenericWebApplicationContext@4a22f9e2
10:38:09.813 [main] INFO  org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter - Looking for @ControllerAdvice: org.springframework.web.context.support.GenericWebApplicationContext@7a4ccb53: startup date [Mon Sep 28 10:38:09 MDT 2015]; parent: org.springframework.web.context.support.GenericWebApplicationContext@4a22f9e2
10:38:09.840 [main] INFO  org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Mapped URL path [/public/**] onto handler 'org.springframework.web.servlet.resource.ResourceHttpRequestHandler#0'
10:38:09.844 [main] INFO  org.springframework.web.servlet.handler.SimpleUrlHandlerMapping - Root mapping to handler of type [class org.springframework.web.servlet.mvc.ParameterizableViewController]
10:38:09.889 [main] INFO  org.springframework.mock.web.MockServletContext - Initializing Spring FrameworkServlet ''
10:38:09.889 [main] INFO  org.springframework.test.web.servlet.TestDispatcherServlet - FrameworkServlet '': initialization started
10:38:09.898 [main] INFO  org.springframework.test.web.servlet.TestDispatcherServlet - FrameworkServlet '': initialization completed in 9 ms
10:38:09.912 [main] WARN  org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [contract/test] in DispatcherServlet with name ''
@RequestMapping("/contract")
...
@RequestMapping("/{name}")
@Test
public void getAccount() throws Exception {
    mockMvc.perform(get("/contract/{name}", "test")).andExpect(status().isOk());
}
10:38:09.912 [main] WARN  org.springframework.web.servlet.PageNotFound - No mapping found for HTTP request with URI [contract/test] in DispatcherServlet with name ''