Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Spring Boot REST Controller returns 404 when deployed on external tomcat 9 server

I have a spring boot rest web application which works perfectly on the embedded server. However after following the steps mentioned in the is blog https://docs.spring.io/spring-boot/docs/current/reference/htmlsingle/#howto-create-a-deployable-war-file, I got a 404 error message whan I send a resquest to a resource on the server.I have used java 1.8.0_212 locally and used java 1.8.0_131 and deployed my app on tomcat 9 on the server. One thing that's puzzling me is that the repositories which extends CrudRepository can be accessed. Below is my application's entry point.

@SpringBootApplication
@ComponentScan(basePackages = "com.dbe.ref")
public class RefmsApplication extends SpringBootServletInitializer {

    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder builder) {
        return builder.sources(RefmsApplication.class);
    }

    public static void main(String[] args) {
        SpringApplication.run(RefmsApplication.class, args);
    }

and also my pom.xml

<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
    xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <groupId>com.dbe.ref</groupId>
    <artifactId>refms</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

    <name>refms</name>
    <description>project for Rural electrification fund</description>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>LATEST</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
        <start-class>com.RefmsApplication</start-class>
    </properties>

    <dependencies>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-jpa</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-rest</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>com.oracle</groupId>
            <artifactId>ojdbc6</artifactId>
            <version>11.2.0.4</version>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>jquery</artifactId>
            <version>2.2.1</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>angularjs</artifactId>
            <version>1.4.10</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>bootstrap</artifactId>
            <version>3.3.4</version>
        </dependency>

        <dependency>
            <groupId>org.webjars</groupId>
            <artifactId>font-awesome</artifactId>
            <version>4.7.0</version>
        </dependency>
        <dependency>
            <groupId>eu.michael-simons</groupId>
            <artifactId>wro4j-spring-boot-starter</artifactId>
            <version>0.3.4</version>
        </dependency>
        <dependency>
            <groupId>net.sf.jasperreports</groupId>
            <artifactId>jasperreports</artifactId>
            <version>6.4.0</version>
        </dependency>
        <dependency>
            <groupId>org.apache.poi</groupId>
            <artifactId>poi</artifactId>
            <version>3.14</version>
        </dependency>

        <dependency>
            <groupId>net.sourceforge.dynamicreports</groupId>
            <artifactId>dynamicreports-core</artifactId>
            <version>5.0.0</version>
        </dependency>

    </dependencies>

    <build>
        <finalName>refms</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>

</project>

Here is part of the log:

2017-09-19 10:38:20.564  INFO 6660 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'errorPageFilter' to: [/*]
2017-09-19 10:38:20.565  INFO 6660 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'characterEncodingFilter' to: [/*]
2017-09-19 10:38:20.566  INFO 6660 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'hiddenHttpMethodFilter' to: [/*]
2017-09-19 10:38:20.568  INFO 6660 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'httpPutFormContentFilter' to: [/*]
2017-09-19 10:38:20.568  INFO 6660 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'requestContextFilter' to: [/*]
2017-09-19 10:38:20.571  INFO 6660 --- [           main] .s.DelegatingFilterProxyRegistrationBean : Mapping filter: 'springSecurityFilterChain' to: [/*]
2017-09-19 10:38:20.571  INFO 6660 --- [           main] o.s.b.w.servlet.FilterRegistrationBean   : Mapping filter: 'configurableWroFilter' to urls: [/wro4j/*]
2017-09-19 10:38:20.572  INFO 6660 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServletRegistration' to [/refms/*]
2017-09-19 10:38:20.573  INFO 6660 --- [           main] o.s.b.w.servlet.ServletRegistrationBean  : Mapping servlet: 'dispatcherServlet' to [/]
like image 434
MikiEthiopia Avatar asked Sep 18 '17 13:09

MikiEthiopia


People also ask

Can spring boot application be deployed to external tomcat server?

Basically, you need to extend your Application with SpringBootServletInitializer like below mention code snippet. After doing all these steps you will be able to run your Spring Boot Application WAR on an external tomcat.

How do I fix HTTP Status 404 not found in tomcat?

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.

Why does spring boot say 404 error?

In this quick article, we explained how to debug 404 errors in Spring MVC. We went through the two most common reasons for receiving a 404 response from our Spring application. The first was using an incorrect URI while making the request. The second was mapping the DispatcherServlet to the wrong url-pattern in web.

Can we override tomcat server in spring boot?

The Spring Boot framework provides the default embedded server (Tomcat) to run the Spring Boot application. It runs on port 8080. It is possible to change the port in Spring Boot.

How to deploy a Spring Boot REST application to a standalone Tomcat?

To demonstrate how to deploy a Spring Boot REST application to a standalone Tomcat 10 server, in this tutorial, we will do the following: Deploy WAR file to a standalone Tomcat 10. Use the following link to download Tomcat 10. For windows users, add the following to environment variables with the name CATALINA_HOME.

What is the default login URL for spring Tomcat?

If unspecified, it defaults to /login. Spring Security framework intercepts that URL and gets login and password parameter. When you deploy your application into Tomcat the URL depends on the context path under which your application gets deployed.

Why is my Tomcat application getting a 404 error?

If you would do so your application will run similar to the embedded Tomcat instance; on the root context. This is relative to the context root of your application. So in case you did not rename your war file, the authentication endpoint on which you receive a 404 is probably going to be under:

Can we use embedded Tomcat server in Spring Boot?

If you are familiar with Spring boot then you already know spring boot has embedded Tomcat Server to run the application, which we generally use to run our application while coding or learning. But when comes to deployment on the production server or some other external server then we cannot use the embedded tomcat server.


3 Answers

Check if the

<build>
     <finalName>refms</finalName>
         ...
</build>

in the pom.xml corresponds with the

server.contextPath=/refms

in the application.properties and check the

<Context path="/refms"/>

in the context.xml. Perhaps your root context in tomcat after deploy was different.

Additionally, change this dependence

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId> 
</dependency>

by this

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-web</artifactId>
    <exclusions>
        <exclusion>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
        </exclusion>
    </exclusions>   
</dependency>

for external tomcat

like image 148
Ednilson Campos Avatar answered Oct 21 '22 11:10

Ednilson Campos


There is a little difference when you deploy application on embedded server and external server by default.

With embedded server you can access you application by using:

http://localhost:<port>/<resourceName>

While if you deploy war in another container then you need to add application name with version like:

http://localhost:<port>/<applicationNameWithVersion>/<resourceName>

For example if you deploy this example then URL for embedded server is:

http://localhost:8080/greeting

And URL for Externally deployed application if like:

http://localhost:8999/gs-rest-service-0.1.0/greeting

Note: This URL is of my application server so it may have some changes for yours.

Comment if you need help.

like image 23
Sunil Kanzar Avatar answered Oct 21 '22 10:10

Sunil Kanzar


Check the root package in src/main/java, it should be the same as the package name mentioned in the POM groupid ie. com.dbe.ref if there is any mismatch than the same issue occurs.

like image 1
Vikas Bharti Avatar answered Oct 21 '22 12:10

Vikas Bharti