Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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).

like image 438
xelamitchell Avatar asked Jan 22 '15 12:01

xelamitchell


People also ask

Why does tomcat say error 404?

This error indicates that the server could not find the desired resource. This resource can be any file such as JSP, HTML, or image resource. Usually, the resource is present, but it is referenced incorrectly. In most cases, you can fix this by correcting the URL.

Can we use spring boot for standalone application?

Spring Boot makes it easy to create stand-alone, production-grade Spring-based applications. Spring Boot provides various starters for building standalone or more traditional war deployments.

Can I deploy spring boot jar to tomcat?

You can not deploy Jar to tomcat and expect it to load your web application.


2 Answers

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).

like image 86
M. Deinum Avatar answered Oct 13 '22 19:10

M. Deinum


check java -version means the if you complied war in java 8 and tomcat is running on java 7 then it doesn't work.

like image 45
Jeet Singh Parmar Avatar answered Oct 13 '22 19:10

Jeet Singh Parmar