java – 修复:“找不到映射”尝试使用Spring-MVC设置RESTfull接口
内容导读
互联网集市收集整理的这篇技术教程文章主要介绍了java – 修复:“找不到映射”尝试使用Spring-MVC设置RESTfull接口,小编现在分享给大家,供广大互联网技能从业者学习和参考。文章包含2874字,纯文字阅读大概需要5分钟。
内容图文
![java – 修复:“找不到映射”尝试使用Spring-MVC设置RESTfull接口](/upload/InfoBanner/zyjiaocheng/704/5c704c427ef742bd9014fa78f7f3ed9d.jpg)
这个问题解决了.有人创建了一个名为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)似乎没有被任何控制器“捕获”.我究竟做错了什么?
我不知道还能尝试什么.我正在使用Java 1.7.0_15,Tomcat 7.0.34和Spring 3.1.4.RELEASE
在我的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中:
<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但仍然无效:
@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”)添加到控制器类
内容总结
以上是互联网集市为您收集整理的java – 修复:“找不到映射”尝试使用Spring-MVC设置RESTfull接口全部内容,希望文章能够帮你解决java – 修复:“找不到映射”尝试使用Spring-MVC设置RESTfull接口所遇到的程序开发问题。 如果觉得互联网集市技术教程内容还不错,欢迎将互联网集市网站推荐给程序员好友。
内容备注
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 gblab@vip.qq.com 举报,一经查实,本站将立刻删除。
内容手机端
扫描二维码推送至手机访问。