Java Spring MVC@RequestMapping不工作
我有一个奇怪的场景,在这个场景中,除非我将DispatcherServlet映射到web.xml中的/*,否则不会调用我的控制器。我已使用RequestMapping定义了一个控制器:Java Spring MVC@RequestMapping不工作,java,spring-mvc,Java,Spring Mvc,我有一个奇怪的场景,在这个场景中,除非我将DispatcherServlet映射到web.xml中的/*,否则不会调用我的控制器。我已使用RequestMapping定义了一个控制器: @Controller public class UserController { @RequestMapping(value = "/rest/users", method = RequestMethod.GET) public ModelAndView getUsers(HttpServ
@Controller
public class UserController {
@RequestMapping(value = "/rest/users", method = RequestMethod.GET)
public ModelAndView getUsers(HttpServletRequest request) throws RestException {
...
}
}
和应用程序上下文:
<?xml version="1.0" encoding="UTF-8"?>
<beans xmlns="http://www.springframework.org/schema/beans"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:util="http://www.springframework.org/schema/util"
xmlns:context="http://www.springframework.org/schema/context"
xmlns:mvc="http://www.springframework.org/schema/mvc"
xsi:schemaLocation="
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-2.5.xsd
http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-2.5.xsd
http://www.springframework.org/schema/util http://www.springframework.org/schema/util/spring-util-2.5.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">
<context:component-scan base-package="com.test.rest.controller" />
最后,它被映射到web.xml中:
<servlet>
<servlet-name>rest-servlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>/WEB-INF/restContext.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>rest-servlet</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
RESTservlet
org.springframework.web.servlet.DispatcherServlet
上下文配置位置
/WEB-INF/restContext.xml
1.
RESTservlet
/*
这与预期一样有效,即我可以向/rest/用户发出请求。但是,如果我将web.xml映射更改为:
<servlet-mapping>
<servlet-name>rest-servlet</servlet-name>
<url-pattern>/rest/*</url-pattern>
</servlet-mapping>
RESTservlet
/休息/*
我收到一个MVC错误:
WARN servlet.PageNotFound:找不到URI为的HTTP请求的映射
名为“rest servlet”的DispatcherServlet中的[/rest/users]
这看起来真的很奇怪,因为错误表明请求被映射到DispatcherServlet,但唯一改变的是servlet映射
还有其他人遇到过这种情况吗?Dispatcher servlet是Spring MVC的主要servlet。它处理所有请求,到达您的应用程序,并使用自己的路由引擎将其分派给控制器。如果你把它改成
<url-pattern>/rest/*</url-pattern>
/rest/*
那么您的请求应该是这样的rest/rest/users
常见模式-允许dispatcher servlet处理所有传入的请求(第一个配置有效)谢谢Anton,我面临的唯一问题是我在MVC dispatcher servlet(GWT stuff等)旁边定义了其他servlet,所以我不想通过它路由所有内容。我想我需要看看一些url重写选项,您可以在servlet声明中定义一些url重写选项。在您的场景中,您可以将SpringMVC映射到/rest,并将控制器映射到/users@Anton“将SpringMVC映射到/rest”是什么意思?你是说调度器servlet?他的控制器看起来像是使用SpringMVC,所以我对你的区别有点困惑。我自己也有类似的问题。@TobyHobson我更喜欢从
/app/rpc/*
和/app/int/rpc/*
提供GWT,其中int
是内部(登录后)rpc,可以通过Spring安全URL规则轻松隔离。