从Spring 4.2.7迁移到5.3.2后,加载了none个applicationcontext(web/service)个文件,RESTful个服务执行失败

After migrating from Spring 4.2.7 to 5.3.2, none of applicationcontext(web/service) files are loaded and RESTful services are failed to execute

Post 从 Spring 4.2.7 迁移到 5.3.2 none 我们应用程序的 REST API 正在触发。例如:我进行的每个 REST 调用在 stp.log 中得到的错误是:

No mapping for GET /application relative path/XXXX/YYYY.rest

以下是web.xml中的重要配置:

Servlet 对象和 servlet url 映射:

<servlet>
    <servlet-name>restServiceExporter</servlet-name>
    <servlet-class>org.springframework.web.servlet.DispatcherServlet</servlet-class>
    <run-as>
        <role-name>AllAuthenticated</role-name>
    </run-as>
</servlet>
<servlet-mapping>
    <servlet-name>restServiceExporter</servlet-name>
    <url-pattern>*.rest</url-pattern>
</servlet-mapping>
<servlet-mapping>
    <servlet-name>restServiceExporter</servlet-name>
    <url-pattern>*.admin</url-pattern>
</servlet-mapping>

restServiceExporter-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" 
 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">
<bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
 </bean>
 <bean class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter">  
  <property name="messageConverters">
   <list>
    <bean class="org.springframework.http.converter.ByteArrayHttpMessageConverter" />
    <bean class="org.springframework.http.converter.StringHttpMessageConverter" />
    <bean class="org.springframework.http.converter.FormHttpMessageConverter" />
    <bean class="org.springframework.http.converter.xml.SourceHttpMessageConverter" />
    <bean class="org.springframework.http.converter.json.MappingJackson2HttpMessageConverter" />
   </list>
  </property>
 </bean>
 <bean id="conversion-service" class="org.springframework.format.support.FormattingConversionServiceFactoryBean" /> 
</beans>

web.xml 上下文配置位置:

<context-param>
    <param-name>contextConfigLocation</param-name>
    <param-value>
        WEB-INF/cxf.xml,
        classpath*:META-INF/<module-name>/applicationContextWeb.xml,
        classpath*:META-INF/dataAccessContext-inContainer.xml,
        classpath*:META-INF/<module-name>/applicationContextService.xml,
        classpath*:META-INF/<module-name>/applicationContextWeb.xml,
    </param-value>
</context-param>

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

    <context:component-scan base-package="<rest Controller package>" use-default-filters="false">
        <context:include-filter expression="org.springframework.stereotype.Controller" type="annotation" />
    </context:component-scan>  
</beans>

当我仔细比较 stp.log 这里是我发现的区别:

Spring 4.2.7 stp.log:

2022-01-11 02:09:59,402 main    INFO    web.context.ContextLoader   Root WebApplicationContext: initialization started
2022-01-11 02:10:00,900 main    INFO    context.support.XmlWebApplicationContext    Refreshing Root WebApplicationContext: startup date [Tue Jan 11 02:10:00 EST 2022]; root of context hierarchy
2022-01-11 02:10:02,069 main    INFO    factory.xml.XmlBeanDefinitionReader Loading XML bean definitions from ServletContext resource [/WEB-INF/cxf.xml]
2022-01-11 02:10:04,047 main    INFO    factory.xml.XmlBeanDefinitionReader Loading XML bean definitions from URL [jar:file:/C:/stp/app/webapps/ourApp/WEB-INF/lib/XYZ.jar!/META-INF/module-name/applicationContextWeb.xml]
2022-01-11 02:10:04,176 main    INFO    factory.xml.XmlBeanDefinitionReader Loading XML bean definitions from URL [jar:file:/C:/stp/app/webapps/ourApp/WEB-INF/lib/XYZ.jar!/META-INF/module-name/restServices.xml]

Spring 5.3.2 stp.log:

2022-01-11 02:20:50,506 main    INFO    web.context.ContextLoader   Root WebApplicationContext: initialization started 

在这行日志之后,我没有看到任何特定于 xml 的 applicationcontext 被加载。

非常感谢任何有关此问题的线索。

我能够使用 Spring 5.2.19.RELEASE 版本而不是 Spring5.3.2 解决这个问题,因为我们需要读取所有 applicationContext 和 restServices.xml 位于多个 jar 中的文件,我必须将“DetectHandlerMethodsInAncestorContexts”属性 添加到 RequestMappingHandlerMapping bean class,如下所示:

<bean
    class="org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerMapping">
    <property name="order"><value>98</value></property>
    <property name="DetectHandlerMethodsInAncestorContexts" value="true" />
</bean>

早些时候在 Spring 4.2.7.RELEASE 我们的调度程序 servlet xml 如下所示:

<bean class="org.springframework.web.servlet.mvc.annotation.DefaultAnnotationHandlerMapping">
  <property name="order" value="0" />
  <property name="detectHandlersInAncestorContexts" value="true" />
 </bean>