Java Spring MVC Rest:在 DispatcherServlet 中找不到带有 URI [/ecommerce-api/rest/checkout] 的 HTTP 请求的映射
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18645877/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Spring MVC Rest: No mapping found for HTTP request with URI [/ecommerce-api/rest/checkout] in DispatcherServlet
提问by Alexio Cassani
(probably) solved`or at least it seems to be. I'm not really sure where it was the problem. For sure the config suggested by Biju Kunjummen, it's working and seems to me cleaner. What I'm doing now, to not generate mess is to work only inside Eclipse, sometimes cleaning the projects and never using maven to package & deploy it (at least during day to day programming, I guess with some robust maven script or CI Server everything will work fine too).
(可能)解决了`或者至少看起来是这样。我不太确定问题出在哪里。可以肯定的是 Biju Kunjummen 建议的配置,它正在工作并且在我看来更干净。我现在正在做的,不产生混乱是只在 Eclipse 中工作,有时清理项目并且从不使用 maven 来打包和部署它(至少在日常编程中,我想使用一些强大的 maven 脚本或 CI 服务器一切都会好起来的)。
I'm trying to setup a Rest API with Spring MVC. I've read a lot of documentation but I'm still getting the error in the subject:
我正在尝试使用 Spring MVC 设置 Rest API。我已经阅读了很多文档,但我仍然收到该主题的错误:
No mapping found for HTTP request with URI [/ecommerce-api/rest/checkout] in DispatcherServlet
在 DispatcherServlet 中找不到带有 URI [/ecommerce-api/rest/checkout] 的 HTTP 请求的映射
The problem is exactly the same reported (and solved) in other similar questions, like this one FIXED: "No mapping found" Trying to set up a RESTfull interface using Spring-MVC
该问题与其他类似问题中报告(并已解决)的问题完全相同,例如此问题已修复:“未找到映射”尝试使用 Spring-MVC 设置 RESTfull 接口
The really strange thing is that, without apparently changing anything in my code, sometime it works and sometimes it doesn't. I'm pretty sure nothing changes between the two moments, since for example sometime I'm physically away then I come back and it stops working.
真正奇怪的是,在没有明显改变我的代码中的任何内容的情况下,有时它会起作用,有时却不起作用。我很确定这两个时刻之间没有任何变化,因为例如有时我身体不在,然后我回来并且它停止工作。
At this particular moment, my code is the following:
在这个特殊时刻,我的代码如下:
web.xml
网页.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
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
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");
}
}
A snippet of the log file of the application server (VFabric but I've also tried with Tomcat 7) when I try to GET ecommerce-api/rest/checkout:
当我尝试GETecommerce -api/rest/checkout时,应用程序服务器的日志文件片段(VFabric,但我也尝试过使用 Tomcat 7):
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
I really don't know what to do since it worked since last time I shut down my Mac and until that moment I thought to know how to do it.
我真的不知道该怎么做,因为自从我上次关闭 Mac 后它就开始工作了,直到那一刻我才想知道该怎么做。
采纳答案by Biju Kunjummen
I see one problem, you are loading up the same context file (api-dispatcher-servlet.xml
) twice. Essentially a typical Spring based web application has two application contexts, the first is the ROOT application context loaded up through the ContextLoaderListener, the second is the Web application context loaded up through DispatcherServlet
, in your case both these are pointing to the exact same configuration file which is api-dispatcher-servlet.xml
. I would recommend you do this and see if it fixes the inconsistent behavior:
我看到一个问题,您加载了相同的上下文文件 ( api-dispatcher-servlet.xml
) 两次。本质上,典型的基于 Spring 的 Web 应用程序有两个应用程序上下文,第一个是通过 ContextLoaderListener 加载的 ROOT 应用程序上下文,第二个是通过 ContextLoaderListener 加载的 Web 应用程序上下文DispatcherServlet
,在您的情况下,它们都指向完全相同的配置文件是api-dispatcher-servlet.xml
。我建议你这样做,看看它是否能解决不一致的行为:
.1. Create say a applicationContext.xml
file, for now it need not have any beans defined, later you can put your service related beans in this file.
.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. Load this context through your ContextLoaderListener, in your web.xml:
.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. Load up api-dispatcher-servlet.xml through your DispatcherServlet, you have the right entries for this already, but it may be better to make this explicit this way:
.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>
回答by svobol13
Weird behaviour is often caused by inconsistency in your compiled and source files. I suggest you to perform proper cleaning (assume you are using eclipse). Stop server, then perform Project -> clean. After that clean server. Try restart your application.
奇怪的行为通常是由编译文件和源文件不一致造成的。我建议您进行适当的清洁(假设您使用的是 eclipse)。停止服务器,然后执行 Project -> clean。在那个干净的服务器之后。尝试重新启动您的应用程序。
Other tip is try to debug what is happening when spring is looking for correct handler.
另一个技巧是尝试调试 spring 正在寻找正确的处理程序时发生的事情。
回答by bernard
I am using @RestController
instead of @Controller
and I am getting the same error.
我正在使用@RestController
而不是,@Controller
我遇到了同样的错误。
WARN org.springframework.web.servlet.PageNotFound noHandlerFound - No mapping found for HTTP request with URI [/something/
JsonResponseBody
] in DispatcherServlet with name 'dispatcher'
警告 org.springframework.web.servlet.PageNotFound noHandlerFound -
JsonResponseBody
在 DispatcherServlet 中未找到具有 URI [/something/ ] 的HTTP 请求的映射,名称为“dispatcher”
Almost everywhere I have searched its stated that we don't need to use @ResponseBody
when we use @RestController
, however, this is exactly what is making it work.
几乎所有我搜索过的地方都声明我们在使用@ResponseBody
时不需要使用@RestController
,但是,这正是使它起作用的原因。
Following is working for me and it won't work if I remove @ResponseBody-
以下对我有用,如果我删除 @ResponseBody- 它将不起作用
@RestController
public class MyController {
@RequestMapping(path = "/dummy")
@ResponseBody
public DummyObject getDummy() {
return new DummyObject("Some data");
}
}
I am using Spring MVC 4.x, WebInitiliazer and 100% code-basedapproach.
我正在使用 Spring MVC 4.x、WebInitiliazer 和100% 基于代码的方法。
YMMV
青年会
回答by Yash Bansal
It happened to me also, if your web-app was running fine and have changed nothing to it and it stopped working, then below steps are for you:
这也发生在我身上,如果您的网络应用程序运行良好并且没有对其进行任何更改并且停止工作,那么以下步骤适合您:
If you are running a maven project, try to do maven clean and then maven install and then again try to run.
Try to reconfigure your TomCat Configurations. You can follow http://www.vogella.com/tutorials/EclipseWTP/article.html.
如果您正在运行 maven 项目,请尝试执行 maven clean 然后 maven install 然后再次尝试运行。
尝试重新配置您的 TomCat 配置。您可以关注http://www.vogella.com/tutorials/EclipseWTP/article.html。
Regards
问候