0

从 Spring 4.2.7 迁移到 5.3.2 后,我们应用程序的所有 REST API 都没有触发。例如:我进行的每个 REST 调用都会在 stp.log 中得到错误:

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 

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

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

4

1 回答 1

0

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

<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>
于 2022-02-03T10:20:26.037 回答