diff --git a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc index 1dcb23ad9f6..0235a51d5a7 100644 --- a/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc +++ b/spring-boot-project/spring-boot-docs/src/main/asciidoc/production-ready-features.adoc @@ -1432,6 +1432,53 @@ include::{code-examples}/cloudfoundry/CloudFoundryIgnorePathsExample.java[tag=se +=== Custom context path + +If the server's context-path has been configured to anything other then `/`, the Cloud Foundry endpoints +will not be available at the root of the application. For example, if `server.servlet.context-path=/foo`, +Cloud Foundry endpoints will be available at `/foo/cloudfoundryapplication/*`. + +If you expect the Cloud Foundry endpoints to always be available at `/cloudfoundryapplication/*`, regardless of +the server's context-path, you will need to explicitly configure that in your application. The configuration will differ +depending on the web server in use. For Tomcat, the following configuration can be added: + +[source,java,indent=0] +---- + @Bean + public TomcatEmbeddedServletContainerFactory servletContainerFactory() { + return new TomcatEmbeddedServletContainerFactory() { + @Override + protected void prepareContext(Host host, + ServletContextInitializer[] initializers) { + super.prepareContext(host, initializers); + StandardContext child = new StandardContext(); + child.addLifecycleListener(new Tomcat.FixContextListener()); + child.setPath("/cloudfoundryapplication"); + ServletContainerInitializer initializer = getServletContextInitializer(getContextPath()); + child.addServletContainerInitializer(initializer, Collections.emptySet()); + child.setCrossContext(true); + host.addChild(child); + } + }; + } + + private ServletContainerInitializer getServletContextInitializer(String contextPath) { + return (c, context) -> { + Servlet servlet = new GenericServlet() { + @Override + public void service(ServletRequest req, ServletResponse res) + throws ServletException, IOException { + ServletContext context = req.getServletContext().getContext(contextPath); + context.getRequestDispatcher("/cloudfoundryapplication").forward(req, res); + } + }; + context.addServlet("cloudfoundry", servlet).addMapping("/*"); + }; + } +---- + + + [[production-ready-whats-next]] == What to Read Next If you want to explore some of the concepts discussed in this chapter, you can take a