2

I am currently deploying a dynamic Java application (servlet based) to Cloud Foundry Java buildpack (Tomcat).

We are using context-path based routing. For Spring application we are setting a context path in application-properties files or Dserver.contextPath, and both work fine.

How do we set the context path to non-Spring Java applications to work on a Cloud Foundry Tomcat container? We tried the below options, but we were getting 404 errors while connecting to route.

cf set-env my-application JBP_CONFIG_TOMCAT '{tomcat: { context_path: /first-segment/second-segment }}'
Peter Mortensen
  • 30,738
  • 21
  • 105
  • 131
PRAMOD N
  • 21
  • 1
  • 2
  • 2
    Did you do a `cf restage my-application` after setting the environment variable? This environment variable needs the application to be restaged, restarting is not enough. – dkoper Mar 16 '17 at 22:08

1 Answers1

3

How to set context path to non spring java applications to work on cloud foundry tomcat container?

The syntax that you have should work. I tested cf set-env JBP_CONFIG_TOMCAT '{tomcat: {context_path: "/spring-music"}}' and it worked OK. You can also use cf set-env JBP_CONFIG_TOMCAT '[tomcat: {context_path: "/spring-music"}]' which worked for me too.

Example using manifest.yml:

---
applications:
- name: spring-music
  memory: 512M
  host: spring-music-example
  path: build/libs/spring-music.war
  env:
    JBP_CONFIG_TOMCAT: '[tomcat: {context_path: "/spring-music"}]'

This results in Spring Music being available at http://spring-music-example.example.com/spring-music, instead of the default http://spring-music-example.example.com/.

This also works very nicely in conjunction with path based routing (i.e. cf push --route-path or or cf create-route --path.

If it's not working:

  1. Check that you've restaged your app or applied the change via cf push.
  2. Check that you're using a recent version of the Java build pack. I tested with 3.14 and it worked.
  3. Check your version of the Java build pack's config/tomcat.yml file (i.e this). This file defines the hierarchy of the config options and has occasionally changed. Make sure what you're setting matches that file in your version of the build pack.

Hope that helps!

Daniel Mikusa
  • 13,716
  • 1
  • 22
  • 28