I am exploring few things in tomcat with Cross Context setup as described here. Both my applications are Spring web mvc based web apps, below are version details.
<java.version>21</java.version>
<spring.version>6.1.9</spring.version>
<hibernate.version>6.2.4.Final</hibernate.version>
apache-tomcat-10.1.24
However while accessing another application’s context in a @Controller @GetMapping, I am getting below error.
java.lang.ClassCastException: class org.springframework.http.server.DefaultRequestPath cannot be cast to class org.springframework.http.server.RequestPath (org.springframework.http.server.DefaultRequestPath is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @4816278d; org.springframework.http.server.RequestPath is in unnamed module of loader org.apache.catalina.loader.ParallelWebappClassLoader @287bdaf3)
org.springframework.web.servlet.DispatcherServlet.doService(DispatcherServlet.java:974)
org.springframework.web.servlet.FrameworkServlet.processRequest(FrameworkServlet.java:1014)
org.springframework.web.servlet.FrameworkServlet.doGet(FrameworkServlet.java:903)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:564)
org.springframework.web.servlet.FrameworkServlet.service(FrameworkServlet.java:885)
jakarta.servlet.http.HttpServlet.service(HttpServlet.java:658)
com.nividous.cc.controller.PersonController.includeContent(PersonController.java:48)
java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:103)
java.base/java.lang.reflect.Method.invoke(Method.java:580)
org.springframework.web.method.support.InvocableHandlerMethod.doInvoke(InvocableHandlerMethod.java:255)
org.springframework.web.method.support.InvocableHandlerMethod.invokeForRequest(InvocableHandlerMethod.java:188)
org.springframework.web.servlet.mvc.method.annotation.ServletInvocableHandlerMethod.invokeAndHandle(ServletInvocableHandlerMethod.java:118)
org.springframework.web.servlet.mvc.method.annotation.RequestMappingHandlerAdapter.invokeHandlerMethod(RequestMappingHandlerAdapter.java:926)
I am using below code to access the context of another application.
// Get the ServletContext of the other web application
ServletContext otherContext = request.getServletContext().getContext("/slave");
if (otherContext != null) {
String targetUrl = null;
targetUrl = "/slave" + request.getPathInfo();
// Get the RequestDispatcher for the target resource
RequestDispatcher rd = otherContext.getRequestDispatcher(targetUrl);
if (rd != null) {
// Include the content of the target resource in the response
rd.include(request, response);
}
}