Note that there are some explanatory texts on larger screens.

plurals
  1. POSpring invokes wrong controller mapping
    primarykey
    data
    text
    <p>I'm building very basic mvc application with Spring. It has one controller that should invoke validation on request body. The problem is that if I define the mapping in web.xml it stops finding the right controller, but when I modify the servlet application context Spring star making some new bindings on the fly but this time annotation based validation is ignored. How can I controll mappings in web.xml while still invoking annotation based validation?</p> <p>Here are the details:</p> <p>The controller:</p> <pre><code>@Controller @RequestMapping("/api") public class UserActionsController { @RequestMapping(value="/choice", method = RequestMethod.POST) public @ResponseBody NameValue addUserChoice(@Valid @RequestBody NameValue action) { return action; } } </code></pre> <p>This is the servlet application context:</p> <pre><code>&lt;mvc:annotation-driven/&gt; &lt;context:component-scan base-package="com.my.package" /&gt; &lt;bean class="org.springframework.web.servlet.view.ContentNegotiatingViewResolver"&gt; &lt;property name="mediaTypes"&gt; &lt;map&gt; &lt;entry key="json" value="application/json" /&gt; &lt;/map&gt; &lt;/property&gt; &lt;property name="defaultContentType" value="application/json" /&gt; &lt;property name="defaultViews"&gt; &lt;list&gt; &lt;bean class="org.springframework.web.servlet.view.json.MappingJacksonJsonView" /&gt; &lt;/list&gt; &lt;/property&gt; &lt;/bean&gt; </code></pre> <p>Web xml:</p> <pre><code>&lt;servlet&gt; &lt;servlet-name&gt;action-api&lt;/servlet-name&gt; &lt;servlet-class&gt;org.springframework.web.servlet.DispatcherServlet&lt;/servlet-class&gt; &lt;load-on-startup&gt;1&lt;/load-on-startup&gt; &lt;/servlet&gt; &lt;servlet-mapping&gt; &lt;servlet-name&gt;action-api&lt;/servlet-name&gt; &lt;url-pattern&gt;/*&lt;/url-pattern&gt; &lt;/servlet-mapping&gt; </code></pre> <p><strong>The configuration above is working.</strong> The problem starts when I try to change web.xml so the controller will only be responsible for "/api/*". I change it to <code>&lt;url-pattern&gt;/api/*&lt;/url-pattern&gt;</code>. In that case Spring cannot find the right controller.</p> <pre><code>(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 </code></pre> <p>Modifying the servlet application context helps, Spring now able to find the controller, but validation is not invoked anymore.</p> <pre><code>&lt;bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter"&gt; &lt;property name="alwaysUseFullPath" value="true" /&gt; &lt;property name="messageConverters"&gt; &lt;util:list id="beanList"&gt; &lt;bean class="org.springframework.http.converter.StringHttpMessageConverter"/&gt; &lt;bean class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/&gt; &lt;/util:list&gt; &lt;/property&gt; &lt;/bean&gt; &lt;bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping"&gt; &lt;property name="alwaysUseFullPath" value="true" /&gt; &lt;/bean&gt; &lt;bean class="org.springframework.web.servlet.handler.SimpleUrlHandlerMapping"&gt; &lt;property name="alwaysUseFullPath" value="true" /&gt; &lt;/bean&gt; </code></pre> <p>Here is the log:</p> <pre><code>(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 [com.my.package.controller.UserActionsController@1f86dbd] and 1 interceptor (HandlerMethodInvoker:638) - Reading [com.my.package.model.NameValue] as "application/json" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@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 [com.my.package.model.NameValue@166685b] as "application/json;charset=UTF-8" using [org.springframework.http.converter.json.MappingJacksonHttpMessageConverter@2059ef] (DispatcherServlet:957) - Null ModelAndView returned to DispatcherServlet with name 'action-api': assuming HandlerAdapter completed request handling (DispatcherServlet:913) - Successfully completed request </code></pre> <p>Looks like Spring making some binding on the fly, but this time validators are ignored. I need to have 2 controllers for, say '/api' and '/something'. How can I define it in web.xml so Spring will be able to locate them and to invoke validation?</p> <p>Thanks.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload