修复:“找不到映射”尝试使用Spring-MVC设置RESTfull接口
这个问题解决了。 有人创建了一个名为mvc-dispatcher.xml的文件,但配置不正确。 该文件自动加载,因为它被称为servlet。
我要感谢所有试图帮我解决这个问题的人。 我在这里保留这个问题,因为它实际上解释了如何创建REST接口。 它完美地运作。
我正在尝试使用Spring-MVC设置RESTful接口。 服务器启动没有问题,但每当我尝试调用REST接口时,我都收到消息:
41 WARN [springframework.web.servlet.PageNotFound] No mapping found for HTTP request with URI [/myweb/rest/asd/aaa] in DispatcherServlet with name 'mvc-dispatcher'
我发送的URL(例如http://localhost:8080/myweb/rest/asd/qwe
) http://localhost:8080/myweb/rest/asd/qwe
没有被任何控制器“捕获”。 我究竟做错了什么?
我不知道还能尝试什么。 我正在使用Java 1.7.0_15,Tomcat 7.0.34和Spring 3.1.4.RELEASE
在我的web.xml中:
MyWeb index.xhtml org.springframework.web.context.ContextLoaderListener org.springframework.web.context.request.RequestContextListener mvc-dispatcher org.springframework.web.servlet.DispatcherServlet 1 mvc-dispatcher /rest/* contextConfigLocation /WEB-INF/applicationContext*.xml
在我的applicationContext.xml中:
最后,我的控制器类:
import org.apache.log4j.Logger; import org.springframework.stereotype.Controller; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.RequestMapping; @Controller public class RestController { private static Logger LOG = Logger.getLogger(RestController.class); @RequestMapping("/asd/{test}") public void test(@PathVariable String test) { LOG.info("You sent "+test); } }
尝试更改方法的@RequestMapping但仍然无效:
@RequestMapping(method=RequestMethod.GET) public void test() { LOG.info("You sent something"); }
你的映射是错误的。
您正在请求/rest/asd/aaa
但您的映射是/asd/{test}
。
您需要将@RequestMapping("/asd/{test}")
更改为@RequestMapping("/rest/asd/{test}")
或者将@RequestMapping("/rest")
到控制器类
尝试使用@RequestMapping("/rest")
注释您的控制器类,以使用url处理所有请求
/ web应用/ REST /
您还应该通过org.springframework.web.context.ContextLoaderListener
侦听器配置上下文。 我想你没有加载applicationContext.xml
将此部分添加到您的web.xml
contextConfigLocation /WEB-INF/config/applicationContext.xml org.springframework.web.context.ContextLoaderListener