Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/338.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 未调用Spring拦截器_Java_Spring_Interceptor - Fatal编程技术网

Java 未调用Spring拦截器

Java 未调用Spring拦截器,java,spring,interceptor,Java,Spring,Interceptor,我正在尝试构建一个Spring应用程序,我想记录所有请求/响应。我找到了一些例子,但没有一个有用。我正在尝试创建一个拦截器,该拦截器将记录我需要的所有信息,但从未调用过拦截器。 有人能解释一下我的拦截器为什么不工作吗? 我的web.xml文件 <web-app xmlns="http://java.sun.com/xml/ns/javaee" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xs

我正在尝试构建一个Spring应用程序,我想记录所有请求/响应。我找到了一些例子,但没有一个有用。我正在尝试创建一个拦截器,该拦截器将记录我需要的所有信息,但从未调用过拦截器。
有人能解释一下我的拦截器为什么不工作吗?

我的web.xml文件

<web-app xmlns="http://java.sun.com/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="
        http://java.sun.com/xml/ns/javaee
        http://java.sun.com/xml/ns/javaee/web-app_3_0.xsd"
         version="3.0">

    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>classpath:spring/business-config.xml</param-value>
    </context-param>

    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>

    <servlet>
        <servlet-name>mvc-dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    </servlet>

    <servlet-mapping>
        <servlet-name>mvc-dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>
</web-app>
编辑:
我通过移动其中所有与mvc相关的内容创建了
mvc dispatcher servlet.xml
。但如果没有
,它就无法工作,拦截器仍然是个问题。

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

    <mvc:default-servlet-handler/>
    <mvc:annotation-driven/>
    <mvc:interceptors>
        <mvc:interceptor>
            <mvc:mapping path="/*"/>
            <bean class="ltp.core.security.RequestProcessingInterceptor"/>
        </mvc:interceptor>
    </mvc:interceptors>
</beans>

老问题,但黄金问题,我在spring MVC中遇到了相同的拦截器问题,只是没有调用它们。 找到解决方案后,你应该检查几件事。首先,确保将
放入dispatcher-servlet.xml(而不是applicationContext.xml)中。扩展
HandlerInterceptorAdapter
的拦截器类不需要任何类似Bean的注释,如@Component(因为您是在xml配置中进行此操作的)

一件重要的事情是检查:
xmlns:mvc=”http://www.springframework.org/schema/mvc“

它不应该是Intellij或其他东西自动添加的
/schema/c
/schema/p

这些是我的代码,用于检查所有内容:

public class MyCustomInterceptor extends HandlerInterceptorAdapter {


@Autowired
private IpClientService ipClientService;


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

    // ------ your code here --------

    String reqUri = request.getRequestURI();
    String serviceName = reqUri.substring(reqUri.lastIndexOf("/") +1, reqUri.length());

    ......



    return super.preHandle(request, response, handler);
}

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

    super.postHandle(request, response, handler, modelAndView);
}
}
my dispatcher-servlet.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:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">


    <mvc:interceptors>
        <bean class="...MyCustomInterceptor"/>
    </mvc:interceptors>


    <context:component-scan base-package="... main package"/>

    <!--this will say we are using annotations-->
    <mvc:annotation-driven/>

    <!--this will add the ability to use @Transactional-->
    <tx:annotation-driven/>

    <!--scheduling tasks-->
    <task:annotation-driven/>


    <!--adding resource directory-->
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>



    <!--in here we specify the view resolver and where it should look for views and what it should add
    as suffix to returning value in @RequestMapping (like index.jsp)-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>




</beans>

my web.xml:

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">





    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
                    /WEB-INF/dispatcher-servlet.xml
        </param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>





</web-app>

上下文配置位置
/WEB-INF/applicationContext.xml,
/WEB-INF/dispatcher-servlet.xml
org.springframework.web.context.ContextLoaderListener
调度员
org.springframework.web.servlet.DispatcherServlet
1.
调度员
/

哪种配置?
business config.xml
mvc dispatcher servlet.xml
的记录器级别设置为log INFO level?@M.Deinum这是一个business config.xml。@AmitParashar记录器设置正确。我也有一个断点->没有调用拦截器。好的。请尝试让我知道。我可以问一下为什么在dispatcher-servlet.xml中使用名称空间mcv和avc,而这两个名称空间都指向“@user3366706”吗?你是对的,名称空间用于解决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:tx="http://www.springframework.org/schema/tx"
       xmlns:task="http://www.springframework.org/schema/task" 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.xsd http://www.springframework.org/schema/tx http://www.springframework.org/schema/tx/spring-tx.xsd http://www.springframework.org/schema/task http://www.springframework.org/schema/task/spring-task.xsd">


    <mvc:interceptors>
        <bean class="...MyCustomInterceptor"/>
    </mvc:interceptors>


    <context:component-scan base-package="... main package"/>

    <!--this will say we are using annotations-->
    <mvc:annotation-driven/>

    <!--this will add the ability to use @Transactional-->
    <tx:annotation-driven/>

    <!--scheduling tasks-->
    <task:annotation-driven/>


    <!--adding resource directory-->
    <mvc:resources mapping="/resources/**" location="/WEB-INF/resources/"/>



    <!--in here we specify the view resolver and where it should look for views and what it should add
    as suffix to returning value in @RequestMapping (like index.jsp)-->
    <bean id="viewResolver" class="org.springframework.web.servlet.view.InternalResourceViewResolver">
        <property name="prefix" value="/WEB-INF/views/"/>
        <property name="suffix" value=".jsp"/>
    </bean>




</beans>
<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns="http://xmlns.jcp.org/xml/ns/javaee"
         xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
         xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd"
         version="3.1">





    <context-param>
        <param-name>contextConfigLocation</param-name>
        <param-value>/WEB-INF/applicationContext.xml,
                    /WEB-INF/dispatcher-servlet.xml
        </param-value>
    </context-param>


    <listener>
        <listener-class>org.springframework.web.context.ContextLoaderListener</listener-class>
    </listener>


    <servlet>
        <servlet-name>dispatcher</servlet-name>
        <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
        <load-on-startup>1</load-on-startup>
    </servlet>


    <servlet-mapping>
        <servlet-name>dispatcher</servlet-name>
        <url-pattern>/</url-pattern>
    </servlet-mapping>





</web-app>