Java – Spring Boot Deployed in Tomcat gives 404 but works Stand-alone

javamavenspring-bootspring-mvctomcat

I have been testing Spring Boot with embedded Tomcat for about a month now to build a REST API. Everything was working fine. We now want to deploy the API in a separate development environment which has a couple of other (non-Spring) applications running on a Tomcat container.

I made the changes specified in Converting a Spring Boot JAR Application to a WAR using Maven and Spring Boot Docs.

The deployment goes well (logs are fine, no errors) and looking at Tomcat management i see my application deployed. However, when I attempt to access http://localhost:8080/sophia/users in curl I get 404.

Any help is much appreciated.

UPDATE:

Here are my logs:

Netbeans:

NetBeans: Deploying on Apache Tomcat 8.0.17
profile mode: false
debug mode: false
force redeploy: true

In-place deployment at /home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT

Deployment is in progress…

deploy?config=file%3A%2Ftmp%2Fcontext1845402702541504208.xml&path=/sophia

OK – Deployed application at context path /sophia

Start is in progress…

start?path=/sophia

OK – Started application at context path /sophia

Tomcat:

INFO 10:47:52:703 org.springframework.boot.context.embedded.ServletRegistrationBean – Mapping servlet: 'dispatcherServlet' to [/sophia/*]

INFO 10:47:54:042 org.springframework.boot.SpringApplication – Started application in 8.285 seconds (JVM running for 12087.301)

22-Jan-2015 10:47:54.060 INFO [http-nio-8080-exec-99] org.apache.catalina.startup.HostConfig.deployDescriptor Deployment of configuration descriptor /home/bugz/workspace/server/apache-tomcat-8.0.17/conf/Catalina/localhost/sophia.xml has finished in 12,091 ms

And in sophia.xml for Catalina localhost:

<?xml version="1.0" encoding="UTF-8"?>
<Context antiJARLocking="true" docBase="/home/bugz/workspace/pdcore/sophiaserver/target/sophia-server-1.0.0-SNAPSHOT" path="/sophia"/>

I've tried accessing

  1. http://localhost:8080/sophia/users
  2. http://localhost:8080/sophia-server-1.0.0-SNAPSHOT/users (name of WAR)

The first returns a 404 but with the CORS information from my CORS filter bean. The second return 404 without CORS information (which indicates the application has started and is configured but I do not seem to have access to the Controllers).

Best Solution

When running an application the path to call consists of a couple of parts.

The first is the base URL on which the application is deployed, in your case that is /sophia.

The second is the servlet mapping of the DispatcherServlet in your case that is /sohpia/*.

The third is the mapping of the controller inside the DispatcherServlet, in your example that is /users.

All those things combined create the URL /sophia/sophia/users.

The difference between the deployment as a WAR is that you included a separate URL to deploy on, when running as a jar it, by default, is deployed to / (the root).

You could fix it by putting /sophia as the server.context-path in the application.properties and map the DispatcherServlet to /* or /. That will in both situations give you the URL you want (and expected).

Related Question