2012-04-20 15 views
9

Ho uno strano scenario in cui il mio controller non viene richiamato a meno che non esegua il mapping del servlet del dispatcher su/* in web.xml. Ho definito un controller con un RequestMapping:Spring MVC @RequestMapping non funziona

@Controller 
public class UserController { 

    @RequestMapping(value = "/rest/users", method = RequestMethod.GET) 
    public ModelAndView getUsers(HttpServletRequest request) throws RestException { 
     ... 
    } 
} 

E un contesto di applicazione:

<?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" /> 

Infine, questa viene mappato in 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> 

Questo funziona come previsto per esempio I può fare richieste a/riposo/utenti. Tuttavia se cambio la mappatura web.xml a:

<servlet-mapping> 
    <servlet-name>rest-servlet</servlet-name> 
    <url-pattern>/rest/*</url-pattern> 
</servlet-mapping> 

ottengo un errore MVC:

WARN servlet.PageNotFound: No mapping found for HTTP request with URI [/rest/users] in DispatcherServlet with name 'rest-servlet'.

Sembra davvero strano, perché l'errore indica che la richiesta viene mappato il dispatcher-servlet , tuttavia l'unica cosa che è cambiata è la mappatura servlet.

Qualcun altro ha riscontrato questo?

risposta

16

Il servlet di Dispatcher è il servlet principale di Spring MVC. Gestisce tutte le richieste, arrivando alla tua applicazione, e utilizzando il proprio motore di routing lo invia ai Controller. Se si cambia in

<url-pattern>/rest/*</url-pattern> 

Allora la vostra richiesta deve essere come questo rest/rest/users

Il modello comune - permettere la spedizione servlet per gestire tutte le richieste in arrivo (prima configurazione è valido)

+0

Grazie Anton, l'unico il problema che devo affrontare è che ho altri servlet definiti insieme al servlet di dispatcher MVC (roba GWT ecc.) quindi non voglio instradare tutto attraverso di esso. Credo che avrò bisogno di esaminare alcune opzioni di riscrittura di URL –

+1

È possibile definire un gruppo di nella dichiarazione di servlet. Nel tuo scenario, puoi mappare Spring MVC a/rest e il tuo controller a/users – Anton

+0

Capito ... ha perfettamente senso ora, grazie per il tuo aiuto –

Problemi correlati