【问题标题】:Spring MVC Rest: No mapping found for HTTP request with URI [/ecommerce-api/rest/checkout] in DispatcherServletSpring MVC Rest:在 DispatcherServlet 中找不到带有 URI [/ecommerce-api/rest/checkout] 的 HTTP 请求的映射
【发布时间】:2013-09-05 20:59:43
【问题描述】:

(可能)解决了 `或者至少看起来是这样。我不确定问题出在哪里。可以肯定的是 Biju Kunjummen 建议的配置,它工作正常,在我看来更干净。我现在正在做的,为了不产生混乱,只在 Eclipse 内部工作,有时清理项目,从不使用 maven 打包和部署它(至少在日常编程中,我猜想使用一些强大的 maven 脚本或 CI 服务器一切都会正常工作)。

我正在尝试使用 Spring MVC 设置 Rest API。我已经阅读了很多文档,但我仍然收到主题中的错误:

在 DispatcherServlet 中找不到带有 URI [/ecommerce-api/rest/checkout] 的 HTTP 请求的映射

该问题与其他类似问题中报告(并已解决)的问题完全相同,例如FIXED: "No mapping found" Trying to set up a RESTfull interface using Spring-MVC

真正奇怪的是,在没有明显改变我的代码中的任何内容的情况下,有时它可以工作,有时却不能。我很确定这两个时刻之间没有任何变化,因为例如有时我身体不在,然后我回来并且它停止工作。

在这个特定的时刻,我的代码如下:

web.xml

 <?xml version="1.0" encoding="UTF-8"?>
<web-app version="2.5" xmlns="http://java.sun.com/xml/ns/javaee"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://java.sun.com/xml/ns/javaee http://java.sun.com/xml/ns/javaee/web-app_2_5.xsd">

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        /WEB-INF/api-dispatcher-servlet.xml
    </param-value>
</context-param>


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

<servlet-mapping>
    <servlet-name>api-dispatcher</servlet-name>
    <url-pattern>/rest/*</url-pattern>
</servlet-mapping>

<listener>
    <listener-class>
        org.springframework.web.context.ContextLoaderListener
    </listener-class>
</listener>
<listener>
    <listener-class>
        org.springframework.web.context.request.RequestContextListener
    </listener-class>
</listener>


</web-app>

api-dispatcher-servlet.xml

<?xml version="1.0" encoding="UTF-8" ?>
<beans:beans xmlns="http://www.springframework.org/schema/mvc"
    xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:beans="http://www.springframework.org/schema/beans"
    xmlns:context="http://www.springframework.org/schema/context"
    xmlns:util="http://www.springframework.org/schema/util"
    xmlns:mvc="http://www.springframework.org/schema/mvc"
    xsi:schemaLocation="http://www.springframework.org/schema/mvc 
    http://www.springframework.org/schema/mvc/spring-mvc-3.0.xsd 
    http://www.springframework.org/schema/beans 
    http://www.springframework.org/schema/beans/spring-beans-3.0.xsd 
    http://www.springframework.org/schema/util 
    http://www.springframework.org/schema/util/spring-util-3.0.xsd http://www.springframework.org/schema/context 
    http://www.springframework.org/schema/context/spring-context-3.0.xsd">

    <context:component-scan base-package="com.myapp.api.ecommerce.controller" />
    <context:annotation-config />
    <mvc:annotation-driven />

</beans:beans>

com.myapp.ecommerce.controller.CheckoutController

package com.myapp.api.ecommerce.controller;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import com.myapp.ecommerce.service.checkout.manager.CheckoutManager;
import com.myapp.ecommerce.service.checkout.manager.CheckoutManagerImpl;
import com.myapp.ecommerce.service.checkout.model.Checkout;


@Controller
@RequestMapping("/checkout")
public class CheckoutController {


    private CheckoutManager checkoutManager = new CheckoutManagerImpl();

    @RequestMapping(method = RequestMethod.GET)
    public @ResponseBody Checkout getCheckout() {

            return checkoutManager.findById("514f2a8e20f7a78a1400001f");
    }


}

当我尝试GET ecommerce-api/rest/checkout时,应用程序服务器的日志文件(VFabric,但我也尝试过使用 Tomcat 7)的 sn-p:

2013-09-05 22:31:37,760 DEBUG [tomcat-http--5] servlet.DispatcherServlet (DispatcherServlet.java:823) - DispatcherServlet with name 'api-dispatcher' processing GET request for [/ecommerce-api/rest/checkout]
2013-09-05 22:31:37,763 DEBUG [tomcat-http--5] handler.AbstractHandlerMethodMapping (AbstractHandlerMethodMapping.java:220) - Looking up handler method for path /checkout
2013-09-05 22:31:37,763 DEBUG [tomcat-http--5] handler.AbstractHandlerMethodMapping (AbstractHandlerMethodMapping.java:230) - Did not find handler method for [/checkout]
2013-09-05 22:31:37,764 WARN  [tomcat-http--5] servlet.DispatcherServlet (DispatcherServlet.java:1108) - No mapping found for HTTP request with URI [/ecommerce-api/rest/checkout] in DispatcherServlet with name 'api-dispatcher'
2013-09-05 22:31:37,764 DEBUG [tomcat-http--5] servlet.FrameworkServlet (FrameworkServlet.java:966) - Successfully completed request

我真的不知道该怎么做,因为自从我上次关闭我的 Mac 以来它一直有效,直到那一刻我才想知道该怎么做。

【问题讨论】:

    标签: java spring rest spring-mvc


    【解决方案1】:

    我发现一个问题,您正在加载相同的上下文文件 (api-dispatcher-servlet.xml) 两次。本质上,典型的基于 Spring 的 Web 应用程序有两个应用程序上下文,第一个是通过 ContextLoaderListener 加载的 ROOT 应用程序上下文,第二个是通过 DispatcherServlet 加载的 Web 应用程序上下文,在您的情况下,这两个都指向完全相同配置文件api-dispatcher-servlet.xml。我建议你这样做,看看它是否能解决不一致的行为:

    .1。创建一个applicationContext.xml 文件,现在它不需要定义任何bean,以后你可以把你的服务相关的bean放在这个文件中。

    <beans xmlns="http://www.springframework.org/schema/beans"
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" 
        xsi:schemaLocation="
        http://www.springframework.org/schema/beans 
        http://www.springframework.org/schema/beans/spring-beans.xsd">
    
    
    </beans>
    

    .2。通过 ContextLoaderListener 在 web.xml 中加载此上下文:

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>
            /WEB-INF/applicationContext.xml
        </param-value>
    </context-param>
    
    <listener>
        <listener-class>
            org.springframework.web.context.ContextLoaderListener
        </listener-class>
    </listener>
    

    .3。通过您的 DispatcherServlet 加载 api-dispatcher-servlet.xml,您已经有了正确的条目,但最好以这种方式明确说明:

    <servlet>
        <servlet-name>api-dispatcher</servlet-name>
        <servlet-class>
            org.springframework.web.servlet.DispatcherServlet
        </servlet-class>
            <init-param>
                <param-name>contextConfigLocation</param-name>
                <param-value>/WEB-INF/api-dispatcher-servlet.xml</param-value>
            </init-param>
            <load-on-startup>1</load-on-startup>
    </servlet>
    
    <servlet-mapping>
        <servlet-name>api-dispatcher</servlet-name>
        <url-pattern>/rest/*</url-pattern>
    </servlet-mapping>
    

    【讨论】:

    • 我已按照您的建议更改了 xml 文件(谢谢,顺便说一句),但我仍然得到相同的行为。无论如何,我会保留您的配置,因为它工作了几次(在我还按照@svobol13 的建议清理了项目之后)。
    【解决方案2】:

    奇怪的行为通常是由您的编译文件和源文件不一致引起的。我建议您进行适当的清洁(假设您使用的是 eclipse)。停止服务器,然后执行 Project -> clean。在那干净的服务器之后。尝试重新启动您的应用程序。

    其他提示是尝试调试当 spring 正在寻找正确的处理程序时发生的事情。

    【讨论】:

    • 谢谢,清理项目后,我看到它工作了一段时间。现在它突然又停止了:-(我所做的是:1-停止 vFabric 服务器并清理其工作目录 2-清理项目 3-mvn clean 4-mvn 包 5-再次将其发布到 vFabric 服务器。但是行为仍然是负面的:80% 的时候它不起作用,20% 的时候......
    • 现在重启 Eclipse 后,再次清理项目并再次部署它可以工作...
    【解决方案3】:

    我使用的是@RestController 而不是@Controller,我遇到了同样的错误。

    警告 org.springframework.web.servlet.PageNotFound noHandlerFound - 在名为“dispatcher”的 DispatcherServlet 中找不到具有 URI [/something/JsonResponseBody] 的 HTTP 请求的映射

    几乎在我搜索过的所有地方,它都声明当我们使用@RestController 时不需要使用@ResponseBody,但是,这正是使它起作用的原因。

    以下对我有用,如果我删除 @ResponseBody- 将无法使用-

    @RestController
    public class MyController {
        @RequestMapping(path = "/dummy")
        @ResponseBody
        public DummyObject getDummy() {
            return new DummyObject("Some data");
        }
    }
    

    我正在使用 Spring MVC 4.x、WebInitiliazer 和 100% code-based 方法。

    YMMV

    【讨论】:

      【解决方案4】:

      我也遇到过这种情况,如果您的网络应用运行良好并且没有对其进行任何更改并且停止工作,那么以下步骤适合您:

      1. 如果您正在运行 maven 项目,请尝试执行 maven clean,然后执行 maven install,然后再次尝试运行。

      2. 尝试重新配置您的 TomCat 配置。你可以关注http://www.vogella.com/tutorials/EclipseWTP/article.html

      问候

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2015-06-03
        • 1970-01-01
        • 2017-09-17
        • 2016-06-11
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2021-08-01
        相关资源
        最近更新 更多