0

I have a spring boot application that runs fine on localhost.

I access it with http://localhost:8080/home.

Now I want to deploy the application to a hosting server.

Generated the .war and deployed it there.

Configured http://www.customdomain.com/hostingpath/ to be the root path for the app.

So that should map to what locally is http://localhost:8080/.

Now when I access http://www.customdomain.com/hostingpath/home I'm getting the following error:

Whitelabel Error Page
This application has no explicit mapping for /error, so you are seeing this as a fallback.

Tue Jan 12 17:40:56 BRT 2021
There was an unexpected error (type=Internal Server Error, status=500).

Taking a look at the logs there's this message:

org.thymeleaf.exceptions.TemplateInputException: Error resolving template [home], template might not exist or might not be accessible by any of the configured Template Resolvers

Don't understand how that can be since the home.html was correctly deployed by the .war file to the \templates folder in the hosting server.

So the file is there. And when the app runs on localhost it opens home.html just fine.

So what could be causing that error?

Thanks.

jkfe
  • 549
  • 7
  • 29
  • your home.html is in resources template folder (src/main/resources/templates)? by default you need to put html files in this location. – Luis Costa Jan 12 '21 at 21:48
  • yes, that's exactly where it is. Both on localhost and on the hosting server. On localhost it works. On hosting server it doesn't. – jkfe Jan 12 '21 at 21:50
  • you already open war file with a zip program to confirm that file was in build? – Luis Costa Jan 12 '21 at 21:53
  • I checked in the hosting service folder. It is there. – jkfe Jan 12 '21 at 21:53
  • what is your server, tomcat? – Luis Costa Jan 12 '21 at 21:54
  • yes, it is tomcat. In the hosting service the .war deployed home.html (along with all the files that are locally under the /templates folder) to /WEB-INF/classes/templates. – jkfe Jan 12 '21 at 21:56
  • in pom.xml dependency "spring-boot-starter-tomcat" needs to be defined with provided scope "provided". – Luis Costa Jan 12 '21 at 21:59
  • yes. That is there as well. – jkfe Jan 12 '21 at 22:00
  • and start-class in pom.xml? when you start tomcat you have logs of application start? – Luis Costa Jan 12 '21 at 22:01
  • I think you do not have configured server context path /hostingpath in your app, if you access to http://www.customdomain.com/home it works? – Luis Costa Jan 12 '21 at 22:05
  • Don't have start-class on pom.xml. Is that needed? There's lots of log of tomcat. For instance: Org.apache.tomcat.util.threads.TaskThread$WrappingRunnable.run(TaskThread.java:61) ~[tomcat-util.jar:9.0.4] at java.base/java.lang.Thread.run(Thread.java:834) ~[na:na]. Have to check if there's a start one. But the hosting service has a tomcat start button and that is started. – jkfe Jan 12 '21 at 22:08
  • Right. I don't have /hostingpath in my app. Would that be needed? I think that is equivalent to localhost. So it should map relatively, no? Accessing /customdomain.com/home does not work. – jkfe Jan 12 '21 at 22:10
  • server.servlet.context-path=/hostingpath – Luis Costa Jan 12 '21 at 22:11
  • I do not remember if you really need to define start-class, since 2016 I do not deploy spring-boot application in tomcat (we use docker, k8s or run with java -jar command). But I think you need to define the starter class, because tomcat deployes the war but do not start spring boot application. – Luis Costa Jan 12 '21 at 22:14
  • Accessing /customdomain.com/home I get a 404 Not Found error. Accessing /customdomain.com/hostingpath/ I also get a 404 Not Found Error. But when accessing /customdomain.com/hostingpath/home I get a Internal Server Error, status=500. – jkfe Jan 12 '21 at 22:14
  • tries to add start-class to your @SpringBootApplication class. example: demo.package.Application – Luis Costa Jan 12 '21 at 22:17
  • add xml in a class? Not sure I get where that should be added. – jkfe Jan 12 '21 at 22:20
  • also what should be place in demo.package.Application? – jkfe Jan 12 '21 at 22:21
  • is in pom.xml :) see this answer https://stackoverflow.com/questions/23217002/how-do-i-tell-spring-boot-which-main-class-to-use-for-the-executable-jar – Luis Costa Jan 12 '21 at 22:21
  • "demo.package.Application" is the package and class when you have @SpringBootApplication annotation – Luis Costa Jan 12 '21 at 22:24
  • add example with start-class – Luis Costa Jan 12 '21 at 22:31

2 Answers2

1

See below an complete example with thymeleaf and deploy war into a tomcat server.

pom.xml

<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
https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>
    <groupId>com.stackoverflow</groupId>
    <artifactId>tomcat-thymeleaf</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <packaging>war</packaging>

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

    <properties>
        <java.version>1.8</java.version>
        <start-class>com.stackoverflow.Application</start-class>
    </properties>

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

        <!-- marked the embedded servlet container as provided -->
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>

    </dependencies>

    <build>
        <plugins>
            <!-- Package as an executable jar/war -->
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
    
</project>

hello.html

<!DOCTYPE html>
<html xmlns:th="http://www.thymeleaf.org">
<head>
<meta charset="UTF-8">
<title>Thymeleaf Example</title>
</head>
<body>
    <h2>Hello!!!</h2>
</body>
</html>

HelloController.java

package com.stackoverflow.controllers;

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;

@Controller
public class HelloController {

    @GetMapping("/hello")
    public String sayHello() {
        return "hello";
    }
    
}

Application.java

package com.stackoverflow;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;

@SpringBootApplication
public class Application extends SpringBootServletInitializer {

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

Project hierarchy

enter image description here

Deploy war in tomcat enter image description here

Access to hello page enter image description here

Luis Costa
  • 1,150
  • 7
  • 8
  • I did that and it didn't work. One thing to note though is that I removed the first level for my app. So where your code is package demo.package, my code is just package mypackage. So I ended up with mypackage.Application. By the way, removing that first level was no issue to run the application on the localhost. Not sure that could be what is causing the issue now. – jkfe Jan 12 '21 at 22:36
  • add a complete example tested in a tomcat version 8.5.61 – Luis Costa Jan 12 '21 at 23:19
  • my application is just like yours just with a few more stuff. But everything you put here is just like I have it. My pom.xml has all of the same entries of yours with a few additional ones. My @SpringBootApplication class also extends SpringBootServletInitializer. Project hierarchy is the same. Just with many more files. But it is also controller package under src/main/java and htmls under src/main/resources/templates. On the hosting server the .war was also uploaded to the same level of the ROOT folder and created a folder on that level with all of the files. So looks the same to me. – jkfe Jan 12 '21 at 23:53
  • I think I'm going to try redeploying on the root folder now to see what happens. Thanks a lot for this. It's really helpful. Voted one up for it. Just didn't mark as accepted because I'm not there yet. thank you. – jkfe Jan 12 '21 at 23:53
  • I found the issue! It was a case sensitive issue. My local system is windows, which is not case sensitive. But the hosting server is linux which is case sensitive. When I renamed Home.html to home.html on the hosting server it worked! Incredible where root cause for issues can be in this world :) Thanks again for your time and help. Much appreciated. – jkfe Jan 13 '21 at 00:43
1

I found the issue. It was a case sensitive issue. My local system is windows, which is not case sensitive. But the hosting server is linux which is case sensitive. When I renamed Home.html to home.html on the hosting server it worked!

jkfe
  • 549
  • 7
  • 29