修复:“未找到映射”尝试使用Spring-MVC设置RESTfull接口 [英] FIXED: "No mapping found" Trying to set up a RESTfull interface using Spring-MVC
问题描述
此问题已解决。有人创建了一个名为mvc-dispatcher.xml的文件,但配置不正确。该文件被自动加载,因为它被称为servlet。
This issue is solved. Someone created a file called mvc-dispatcher.xml and it had an incorrect configuration. That file is loaded automatically because it's called the same as a servlet.
我要感谢所有试图帮我解决这个问题的人。我在这里保留这个问题,因为它实际上解释了如何创建REST接口。它运行得很好。
I want to thank everyone who tried to help me fix this issue. I'm keeping this question here, since it actually explains how to create a REST interface. It works perfectly.
我正在尝试使用Spring-MVC建立一个RESTful接口。服务器启动没有问题,但每当我尝试调用REST接口时,我都收到消息:
I'm trying to set up a RESTful interface with Spring-MVC. The server starts without issue, but any time I try to call the REST interface I get the message:
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
for示例)不被任何控制器捕获。我做错了什么?
It seems that the URL I am sending (http://localhost:8080/myweb/rest/asd/qwe
for example) is not 'captured' by any controller. What am I doing wrong?
我不知道我还能尝试什么。我正在使用Java 1.7.0_15,Tomcat 7.0.34和Spring 3.1.4.RELEASE
I do not know what else I could try. I'm using Java 1.7.0_15, Tomcat 7.0.34 and Spring 3.1.4.RELEASE
在我的web.xml中:
In my 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_3_0.xsd" id="WebApp_ID" version="3.0">
<display-name>MyWeb</display-name>
<welcome-file-list>
<welcome-file>index.xhtml</welcome-file>
</welcome-file-list>
<!-- Listener for MVC spring -->
<listener>
<listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
</listener>
<listener>
<listener-class>org.springframework.web.context.request.RequestContextListener</listener-class>
</listener>
<!-- Servlet for MVC spring -->
<servlet>
<servlet-name>mvc-dispatcher</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>mvc-dispatcher</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
<!-- Loading web properties -->
<context-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/applicationContext*.xml</param-value>
</context-param>
在我的applicationContext.xml中:
In my applicationContext.xml:
<context:component-scan base-package="com.myweb.*" />
<context:annotation-config/>
<tx:annotation-driven/>
<mvc:annotation-driven />
最后,我的控制器类:
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但仍然没有work:
Tried changing the method's @RequestMapping but still didn't work:
@RequestMapping(method=RequestMethod.GET)
public void test() {
LOG.info("You sent something");
}
推荐答案
您的映射是错误的。
您正在请求 / rest / asd / aaa
但您的映射是针对 / asd / {test}
。
You are requesting /rest/asd/aaa
but your mapping is for /asd/{test}
.
您需要更改 @RequestMapping(/ asd / {test})
到 @RequestMapping(/ rest / asd / {test})
或者添加 @RequestMapping(/ rest)
到您的控制器类
Or add @RequestMapping("/rest")
to your controller class
这篇关于修复:“未找到映射”尝试使用Spring-MVC设置RESTfull接口的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!