gpt4 book ai didi

java - Spring HandlerInterceptor 调用了两次

转载 作者:塔克拉玛干 更新时间:2023-11-02 08:50:38 24 4
gpt4 key购买 nike

在一个库项目中(使用 Spring 3.2.4)我定义了多个拦截器。 servlet 配置 xml 文件包含在要导入 Web 应用程序的 jar 中。拦截器用于多个 servlet xml,因为它们应用于具有不同拦截器的不同 Dispatcher servlet。

问题是,拦截器被调用了两次,但处理程序( Controller )只被调用了一次。

拦截器在库项目中定义:

public class SomeInterceptor extends HandlerInterceptorAdapter {

@Override
public void afterCompletion(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final Exception ex) throws Exception {
System.out.println("afterCompletionCalled");
}

@Override
public boolean preHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler) throws Exception {
System.out.println("preHandle called");
return true;
}

@Override
public void postHandle(final HttpServletRequest request, final HttpServletResponse response, final Object handler, final ModelAndView modelAndView) throws Exception {
}
}

servlet 配置在 jar 文件中提供,稍后包含在应用程序中。

libservletcfg.xml:

<?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: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-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd
http://www.springframework.org/schema/mvc http://www.springframework.org/schema/mvc/spring-mvc-3.1.xsd">

<context:component-scan base-package="com.example.controller" annotation-config="true" />

<bean id="jacksonMessageConverter" class="org.springframework.http.converter.json.MappingJacksonHttpMessageConverter"/>

<bean class="org.springframework.web.servlet.mvc.annotation.AnnotationMethodHandlerAdapter">
<property name="messageConverters">
<list>
<ref bean="jacksonMessageConverter"/>
</list>
</property>
</bean>

<mvc:annotation-driven />

<mvc:interceptors>
<mvc:interceptor>
<mvc:mapping path="/**"/>
<bean class="com.example.SomeInterceptor"/>
</mvc:interceptor>
</mvc:interceptors>

</beans>

在 Web 应用程序中,我只在 servlet 配置中包含了 libservletcfg.xml。

servletConfig.xml:

<?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:context="http://www.springframework.org/schema/context"
xsi:schemaLocation="http://www.springframework.org/schema/beans http://www.springframework.org/schema/beans/spring-beans-3.1.xsd
http://www.springframework.org/schema/context http://www.springframework.org/schema/context/spring-context-3.1.xsd">

<import resource="classpath*:libservletcfg.xml"/>

<context:component-scan base-package="com.example.app.controller" annotation-config="true" />

</beans>

此 servletConfig.xml 用作 web.xml 中 Dispatcher servlet 的上下文配置:

<servlet>
<servlet-name>someServlet</servlet-name>
<servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
<init-param>
<param-name>contextConfigLocation</param-name>
<param-value>classpath*:servletConfig.xml</param-value>
</init-param>
<load-on-startup>1</load-on-startup>
</servlet>

<servlet-mapping>
<servlet-name>someServlet</servlet-name>
<url-pattern>/someUrl/*</url-pattern>
</servlet-mapping>

谁能告诉我为什么拦截器被调用两次或配置有任何问题(这可能导致意外行为)?

编辑

Controller 调用两次的示例:

@Controller
@RequestMapping(value = "/someUrl")
public class SampleController {

@RequestMapping(method = RequestMethod.POST)
public @ResponseBody SampleResponseBody sampleMethod(@RequestBody final SampleRequestBody pSampleRequestBody) {
final SampleResponseBody response = new SampleResponseBody();
return response;
}

}

最佳答案

检查你没有另一个 libservletcfg.xml 闲逛。 classpath*: 将匹配所有这些。

关于java - Spring HandlerInterceptor 调用了两次,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19316038/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com