Spring MVC handler interceptor not starting

I have an interceptor class:

package cz.coffeeexperts.feedback.server.web.interceptors;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.servlet.handler.HandlerInterceptorAdapter;

public class RestAuthorizationInterceptor extends HandlerInterceptorAdapter {

    @Override
    public boolean preHandle(HttpServletRequest request, 
            HttpServletResponse response, Object handler)
        throws Exception {

        System.out.println("fuu");
        response.setStatus( HttpServletResponse.SC_UNAUTHORIZED );
        return false;
    }
}

I configured it inside my spring -webmvc.xml as follows:

<beans xmlns="http://www.springframework.org/schema/beans"
       xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
       xmlns:context="http://www.springframework.org/schema/context"
       xmlns:mvc="http://www.springframework.org/schema/mvc"
       xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans.xsd

       http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context.xsd http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.2.xsd">

    <mvc:annotation-driven/>

    <mvc:interceptors>
     <mvc:interceptor>
       <mvc:mapping path="/rest/api/01/status" />
       <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
     </mvc:interceptor>
    </mvc:interceptors>     

</beans>

However, when I go to http://localhost:8080/myserver/rest/api/01/status, I get a regular response with a status code of 200 (same as before I added the interceptor). In addition, the message “fuu” is not printed (therefore, the preHandle method is not called).

Any ideas? I started doing this with this example: http://javapapers.com/spring/spring-mvc-handler-interceptor/ , but all the other examples look the same, I can’t find where I am wrong.

I am using spring 3.2.4.RELEASE


Important editing, it works with this:

<mvc:interceptors>
 <mvc:interceptor>
   <mvc:mapping path="/**" />
   <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>  

So the question is, what's wrong with my path?

+4
3

, , :

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

@Controller
@RequestMapping(value = "/api")
public class ApiController {
 @RequestMapping(value = "/01/status", method = RequestMethod.GET)
    @ResponseBody
    public ServerStatusJSON getStatus(HttpServletResponse response) {
        ...
    }
}

: http://localhost:8080/myserver/rest/api/01/status :

<mvc:interceptors>
 <mvc:interceptor>
   <mvc:mapping path="/api/01/status" />
   <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>    

PS: , .

+3

, mvc:mapping. :

    <mvc:interceptors>
    <mvc:interceptor>
        <mvc:mapping path="/**"/>
        <bean class="cn.mmd.micro.common.TokenInterceptor">
            <property name="excludeUrls">
                <list>
                    <value>/app/token</value>
                </list>
            </property>
        </bean>
    </mvc:interceptor>
</mvc:interceptors>
+1

, libik. ,

@Controller
@RequestMapping(value = "/api")
public class ApiController {
 @RequestMapping(value = "/01/status", method = RequestMethod.GET)
    @ResponseBody
    public ServerStatusJSON getStatus(HttpServletResponse response) {
        ...
    }
}

, HandlerMapping. RequestMappingHandlerMapping (Spring 3.1+ mvc:annotation-driven) DefaultAnnotationHandlerMapping. /api/01/status , .

<mvc:interceptors>
 <mvc:interceptor>
   <mvc:mapping path="/api/01/status" />
   <bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterceptor" />
 </mvc:interceptor>
</mvc:interceptors>   

If you want it to be applied to all templates, you can just do it <mvc:mapping path="/**"/>- it will match all URLs (including subfolders) or you can just call interceptors for all HandlerMappings -

<mvc:interceptors> 
<bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterc‌​eptor" /> 
</mvc:interceptors>
+1
source

Source: https://habr.com/ru/post/1538064/


All Articles