我正在用Spring構建非常基本的mvc應用程序。它有一個控制器應該在請求主體上調用驗證。問題是,如果我在web.xml中定義映射,它會停止找到正確的控制器,但是當我修改servlet應用程序上下文時,Spring star會動態地創建一些新的綁定,但這次基於註釋的驗證將被忽略。如何在仍然調用基於註解的驗證的同時在web.xml中控制映射?Spring調用錯誤的控制器映射
下面是詳細信息:
控制器:
@Controller
@RequestMapping("/api")
public class UserActionsController {
@RequestMapping(value="/choice", method = RequestMethod.POST)
public @ResponseBody NameValue addUserChoice(@Valid @RequestBody NameValue action)
{
return action;
}
}
這是servlet應用程序上下文:
<mvc:annotation-driven/>
<context:component-scan base-package="com.my.package" />
<bean
class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver">
<property name="mediaTypes">
<map>
<entry key="json" value="application/json" />
</map>
</property>
<property name="defaultContentType" value="application/json" />
<property name="defaultViews">
<list>
<bean
class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" />
</list>
</property>
</bean>
網站XML:
<servlet>
<servlet-name>action-api</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<load-on-startup>1</load-on-startup>
</servlet>
<servlet-mapping>
<servlet-name>action-api</servlet-name>
<url-pattern>/*</url-pattern>
</servlet-mapping>
上面的配置正在工作。當我嘗試更改web.xml時,問題就開始了,所以控制器只負責「/ api/*」。我將它更改爲<url-pattern>/api/*</url-pattern>
。在這種情況下,Spring無法找到正確的控制器。
(DispatcherServlet:819) - DispatcherServlet with name 'action-api' processing POST request for [/api/choice]
(RequestMappingHandlerMapping:209) - Looking up handler method for path /choice
(RequestMappingHandlerMapping:219) - Did not find handler method for [/choice]
(PageNotFound:1080) - No mapping found for HTTP request with URI [/api/choice] in DispatcherServlet with name 'action-api'
(DispatcherServlet:913) - Successfully completed request
修改servlet應用程序上下文幫助,Spring現在能夠找到控制器,但不再調用驗證。
<bean
class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="alwaysUseFullPath" value="true" />
<property name="messageConverters">
<util:list id="beanList">
<bean class="org.springframework.http.converter.StringHttpMessageConverter"/>
<bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>
</util:list>
</property>
</bean>
<bean
class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
<bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping">
<property name="alwaysUseFullPath" value="true" />
</bean>
這裏是日誌:
(DispatcherServlet:819) - DispatcherServlet with name 'action-api' processing POST request for [/api/choice]
(RequestMappingHandlerMapping:209) - Looking up handler method for path /choice
(RequestMappingHandlerMapping:219) - Did not find handler method for [/choice]
(DefaultAnnotationHandlerMapping:124) - Mapping [/api/choice] to HandlerExecutionChain with handler [[email protected]] and 1 interceptor
(HandlerMethodInvoker:638) - Reading [com.my.package.model.NameValue] as "application/json" using [org.springf[email protected]2059ef]
(HandlerMethodInvoker:173) - Invoking request handler method: public com.my.package.model.NameValue com.citypath.dima.controller.UserActionsController.addUserChoice(com.my.package.model.NameValue)
(AnnotationMethodHandlerAdapter:1037) - Written [[email protected]] as "application/json;charset=UTF-8" using [org.springf[email protected]2059ef]
(DispatcherServlet:957) - Null ModelAndView returned to DispatcherServlet with name 'action-api': assuming HandlerAdapter completed request handling
(DispatcherServlet:913) - Successfully completed request
看起來像春天做對飛一些綁定,但這次驗證被忽略。 我需要2個控制器,比如'/ api'和'/ something'。我如何在web.xml中定義它,以便Spring能夠找到它們並調用驗證?
謝謝。
謝謝。 '/ api/api/choice'確實幫助我理解Spring爲什麼找不到控制器。如果我沒有在控制器中定義'api'部分,只需在web.xml中,所有的工作都很好。甚至可以在控制器和方法級映射。就像它想要工作一樣。 – Dima 2012-08-08 10:41:26
這個答案幫助我找到了我的問題的根本原因,這是我的@ComponentScan不包括我的控制器的包 – McLovin 2016-03-28 18:13:33