Cloudfoundry not recognizing your app as Spring Boot? Use empty context path!


Posted by Steven

I spend the bigger part of yesterday figuring out why my Spring Boot app was not recognized as such in Pivotal Cloudfoundry app manager. The Spring Boot logo was not displayed in front of the app name and I couldn't configure the log levels or see the "Trace" and "Thread" tabs. Short answer: I used a custom context path. That doesn't work with the Actuator integration in Cloudfoundry. Read more for details ...

Spring Actuator and Cloudfoundry

Spring Actuator exposes HTTP endpoints in a Spring Boot application that can be used to control certain aspects of the app, like getting health information or changing the log level. Pivotal Cloudfoundry has a really nice integration for Actuator: The app manager, the web UI to control deployed apps, offers new tabs if a Spring Boot application with Actuator is recognized.

Here are sources to set up Actuator in Cloudfoundry:

App not recognized as Spring Boot by Cloudfoundry

There are many articles like this one describing setups in which Cloudfoundry is not able to recognize Spring Boot and/or Actuator. Even Pivotal kind of tries to help, but doesn't provide the missing clue.

After a lot of searching, I found this final piece to the puzzle. To provide Actuator support, Cloudfoundry needs to be able to call the endpoint /cloudfoundryapplication. However, if you set a custom context path like this (in application.yml) ...

  1. server:
  2. port: 8090
  3. servlet:
  4. context-path: /myapp

... Cloudfoundry will not adapt the call to myapp/cloudfoundryapplication. Instead, you will see errors in the network tab of your browser developer tool.

There are two solutions:

  1. Use the default context path "/".
  2. Add the following configuration, taken from the article above. It will make Tomcat provide the endpoint without a context:
  1. @Bean
  2. public TomcatServletWebServerFactory servletWebServerFactory() {
  3. return new TomcatServletWebServerFactory() {
  4.  
  5. @Override
  6. protected void prepareContext(Host host,
  7. ServletContextInitializer[] initializers) {
  8. super.prepareContext(host, initializers);
  9. StandardContext child = new StandardContext();
  10. child.addLifecycleListener(new Tomcat.FixContextListener());
  11. child.setPath("/cloudfoundryapplication");
  12. ServletContainerInitializer initializer = getServletContextInitializer(
  13. getContextPath());
  14. child.addServletContainerInitializer(initializer, Collections.emptySet());
  15. child.setCrossContext(true);
  16. host.addChild(child);
  17. }
  18.  
  19. };
  20. }
  21.  
  22. private ServletContainerInitializer getServletContextInitializer(String contextPath) {
  23. return (c, context) -> {
  24. Servlet servlet = new GenericServlet() {
  25.  
  26. @Override
  27. public void service(ServletRequest req, ServletResponse res)
  28. throws ServletException, IOException {
  29. ServletContext context = req.getServletContext()
  30. .getContext(contextPath);
  31. context.getRequestDispatcher("/cloudfoundryapplication").forward(req,
  32. res);
  33. }
  34.  
  35. };
  36. context.addServlet("cloudfoundry", servlet).addMapping("/*");
  37. };
  38. }

This has been really annoying. I hope this article is of help for others.

Category: 
Share: