2014-04-25 2 views
4

나는 다음과 같은 한 인터셉터 클래스를 실행하지 않습니다스프링 MVC 핸들러 인터셉터가

<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> 

을 그러나 내가 http://localhost:8080/myserver/rest/api/01/status에 갈 때, 나는 상태 코드 200 (인터셉터를 추가하기 전과 동일)에 대한 정규 응답을 얻는다. 또한 메시지 "fuu"는 인쇄되지 않습니다 (따라서 preHandle 메소드가 호출되지 않습니다).

아이디어가 있으십니까? 나는이 예제로 시작했다 : http://javapapers.com/spring/spring-mvc-handler-interceptor/,하지만 다른 모든 예제는 똑같아 보인다, 내가 잘못 갈 수는 없다.

나는 봄 3.2.4.RELEASE


중요 편집을 사용하고,이 작동 :

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

그래서 질문은, 내 길 잘못 무엇입니까? 내 경로가 다음과 같이 정의되어 있기 때문에

+0

모든 로그 ? 오류? 그것의 prehandle로서, 응답은 당신의 정상적인 컨트롤러 메소드에 의해 무시 될 것입니다 - prehandle은 일반적으로 요청을 처리하기위한 것입니다. – NimChimpsky

+0

아무 일도 일어나지 않고 오류도없고 메서드도 호출되지 않습니다 (어떻게 말했는지, System.out.println ("fuu")에 대한 결과는 나타나지 않습니다). 또한 preHandle에 false를 반환하면 컨트롤러 메서드를 가로 챌 것입니다 ... – libik

+0

매핑을 제거 할 때 인터셉터가 작동합니까? – geoand

답변

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>  

추신 : geoand 덕분에 그는 나를 r ight way.

1

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 (스프링 3.1+은 mvc:annotation-driven) 또는 DefaultAnnotationHandlerMapping이됩니다. 여기의 매핑은 정확히 그가 한 일인 /api/01/status 일 것입니다. 이 (서브 패스를 포함한) 모든 URL과 일치합니다 또는 당신은 단순히 모든 HandlerMapping에 대한 인터셉터를 호출 할 수 있습니다 - -

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

당신은 그것이 당신이 단순히 <mvc:mapping path="/**"/>가 할 수있는 패턴을 적용하려면

<mvc:interceptors> 
<bean class="cz.coffeeexperts.feedback.server.web.interceptors.RestAuthorizationInterc‌​eptor" /> 
</mvc:interceptors> 
관련 문제