view raw
James James - 6 months ago 98
Java Question

Spring boot WAR deployed to Tomcat and missing context for static resources

I'm having a problem when deploying a Spring Boot application as a WAR file to a standalone Tomcat 7 server. It builds and deploys fine but when the index.html page tries to load other static resources they are missing the context in the url so fail to load (404).



should be:

Image showing issue

It works fine when using the embedded Tomcat

Similar issues:

Seems to be a similar issue to:
Spring-Boot war external Tomcat context path

However the suggestions in that question did not seem to solve my issue. I wasn't sure about the Tomcat xml file.

Steps followed:

I created a simple sample application and followed the steps in the Spring Boot docs.

The sample code can be seen in this github repo along with steps to reproduce the problem:

Things I've tried so far:

  • Set contextPath in but this only applies to embedded tomcat

  • Tried using a fresh install of Tomcat 7

  • Tried creating a config file in tomcat to force context:

Contents of spring-boot-war-context-issue.xml:


Any advice would be much appreciated!


Update 23/10/2016:

Alex's answer below about using relative URLs without the slash at the start was perfect solution!


Maybe I'm missing something, but isn't it simply caused by the way you defined your url in index.html (the url does not include the context root):

<img src="/app/images/springboot.png" />

Take a look at these:

Use relative uri

You should be able to use a relative uri (without the forward slash):

<img src="app/images/springboot.png" />

get the context root (from the links above)


<img src="${pageContext.request.contextPath}/app/images/springboot.png" />

Or with Javascript:

function getContextPath() {
   return window.location.pathname.substring(0,  window.location.pathname.indexOf("/",2));
var img = new Image();
img.src = getContextPath() + "/app/images/springboot.png";