(可能)解决了`或者至少看起来是这样。我不确定问题出在哪里。可以肯定的是 Biju Kunjummen 建议的配置,它工作正常,在我看来更干净。我现在正在做的,为了不产生混乱,只在 Eclipse 内部工作,有时清理项目,从不使用 maven 打包和部署它(至少在日常编程中,我猜想使用一些强大的 maven 脚本或 CI 服务器一切都会正常工作)。
我正在尝试使用 Spring MVC 设置一个 Rest API。我已经阅读了很多文档,但我仍然收到主题中的错误:
在 DispatcherServlet 中找不到带有 URI [/ecommerce-api/rest/checkout] 的 HTTP 请求的映射
该问题与其他类似问题中报告(并已解决)的问题完全相同,例如已修复:“未找到映射”尝试使用 Spring-MVC 设置 RESTfull 接口
真正奇怪的是,在我的代码中没有明显改变任何东西的情况下,有时它可以工作,有时它不会。我很确定这两个时刻之间没有任何变化,因为例如有时我身体不在,然后我回来并且它停止工作。
在这个特定的时刻,我的代码如下:
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):
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 以来它一直有效,直到那一刻我才想知道该怎么做。