org.springframework.web.servlet.DispatcherServlet noHandlerFound 通过 MockMvc 测试时

Posted

技术标签:

【中文标题】org.springframework.web.servlet.DispatcherServlet noHandlerFound 通过 MockMvc 测试时【英文标题】:org.springframework.web.servlet.DispatcherServlet noHandlerFound when testing through MockMvc 【发布时间】:2015-07-29 15:13:10 【问题描述】:

我已经使用 Spring Mvc 4 定义了一个休息服务,然后通过 MockMvc 对其进行测试。当我通过以下 URL 使用 Tomcat 7 运行服务时返回正确的响应:

http://localhost:8080/SpringServiceSample/service/greeting/Niharika

但是当我运行 Junit 测试时,我的日志中出现 404 错误:

INFO: FrameworkServlet '': initialization completed in 159 ms
May 18, 2015 12:36:02 PM org.springframework.web.servlet.DispatcherServlet noHandlerFound
WARNING: No mapping found for HTTP request with URI [/service/greeting] in DispatcherServlet with name ''

以下是代码:

SpringServiceController.java

package com.test.springservice.controller;

import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/service/greeting")
public class SpringServiceController 
    @RequestMapping(value = "/firstName", method = RequestMethod.GET)
    @ResponseBody
    public String getGreeting(
        @PathVariable String firstName) 
        String result = "Hello " + firstName;
        return result;
    

test-servlet.xml

<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:mvc="http://www.springframework.org/schema/mvc"     xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xmlns:p="http://www.springframework.org/schema/p"
    xsi:schemaLocation="
        http://www.springframework.org/schema/beans     
        http://www.springframework.org/schema/beans/spring-beans-4.0.xsd
        http://www.springframework.org/schema/context
        http://www.springframework.org/schema/context/spring-context-4.0.xsd
        http://www.springframework.org/schema/mvc
        http://www.springframework.org/schema/mvc/spring-mvc-4.0.xsd">
    <context:component-scan base-package="com.test.springservice.controller" />
    <mvc:annotation-driven />
</beans>

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"  xmlns="http://java.sun.com/xml/ns/javaee" xmlns:web="http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd" id="WebApp_ID" version="2.5">
  <display-name>SpringServiceSample</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
  </welcome-file-list>


<servlet>
    <servlet-name>test</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
    <servlet-name>test</servlet-name>
    <url-pattern>/*</url-pattern>
</servlet-mapping>
</web-app>

SpringServiceControllerTest.java

package com.test.springservice.controller;

import static org.springframework.test.web.servlet.result.MockMvcResultHandlers.print;
import static org.springframework.test.web.servlet.setup.MockMvcBuilders.webAppContextSetup;

import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.test.context.junit4.SpringJUnit4ClassRunner;
import org.springframework.test.context.web.WebAppConfiguration;
import org.springframework.test.web.servlet.MockMvc;
import org.springframework.test.web.servlet.request.MockMvcRequestBuilders;
import org.springframework.test.web.servlet.result.MockMvcResultMatchers;
import org.springframework.web.context.WebApplicationContext;

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration
@WebAppConfiguration
public class SpringServiceControllerTest 
    @Autowired
    private WebApplicationContext ctx;

    private MockMvc mockMvc;

    @Before
    public void setup() 
        this.mockMvc = webAppContextSetup(ctx).build();
    

    @Test
    public void testGetGreeting() throws Exception 
        String firstName = "Niharika";
        mockMvc.perform(
            MockMvcRequestBuilders.get("/service/greeting").param(
                    "firstName", firstName))
            .andDo(print())
            .andExpect(MockMvcResultMatchers.status().isOk())
            .andExpect(
                    MockMvcResultMatchers.content().string(
                            "Hello " + firstName));
    

    @Configuration
    public static class TestConfiguration 
        @Bean
        public SpringServiceController springServiceController() 
            return new SpringServiceController();
        
    

请建议我在这里可能做错了什么。

【问题讨论】:

我有类似的问题,我的错误是:[org.springframework.web.servlet.PageNotFound:1136] 在 DispatcherServlet 中找不到带有 URI [/xhr/ipopt/get] 的 HTTP 请求的映射用名字''。但我可以在浏览器中正确访问此网址 【参考方案1】:

尝试将test-servlet.xml添加到@ContextConfiguration注解中,即

@RunWith(SpringJUnit4ClassRunner.class)
@ContextConfiguration(locations="classpath*:test-servlet.xml")
@WebAppConfiguration
public class SpringServiceControllerTest 
    @Autowired
    private WebApplicationContext ctx;

    private MockMvc mockMvc;

顺便在test-servlet.xml中加&lt;mvc:default-servlet-handler /&gt;

【讨论】:

以上是关于org.springframework.web.servlet.DispatcherServlet noHandlerFound 通过 MockMvc 测试时的主要内容,如果未能解决你的问题,请参考以下文章