0

I have a Spring Boot application (using Gradle build tool) that I would like to deploy using Tomcat 10. I create the war, deploy it using Tomcat Manager App (or drag and drop the war under tomcat installation /webapps), and then open it, but I always get 404 with message "The requested resource is not available".
To test if something was wrong with my app, I created a completely new demo app with one controller and endpoint, that should return a string. This didn't work either.
Then I tried to follow a tutorial (https://www.youtube.com/watch?v=OEx_V1q04tg&ab_channel=DailyCoding). I followed it step-by-step, but still I always get a 404 response.
I also tried to find help elsewhere (for example here, https://www.codejava.net/java-ee/servlet/solved-tomcat-error-http-status-404-not-found), but no help.
Here is my demoapp build.gradle:

plugins {
    id 'org.springframework.boot' version '2.4.5'
    id 'io.spring.dependency-management' version '1.0.11.RELEASE'
    id 'java'
    id 'war'
}

group = 'com.example'
version = '0.0.1-SNAPSHOT'
sourceCompatibility = '8'

repositories {
    mavenCentral()
}

dependencies {
    implementation 'org.springframework.boot:spring-boot-starter-web'
    testImplementation 'org.springframework.boot:spring-boot-starter-test'
    providedRuntime 'org.springframework.boot:spring-boot-starter-tomcat'
}

test {
    useJUnitPlatform()
}

Here is DemoApplication.java:

package com.example.demo;

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

@SpringBootApplication
public class DemoApplication extends SpringBootServletInitializer {

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

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

Controller:

package com.example.demo.controllers;

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

@Controller
public class HelloController {

    @RequestMapping("/")
    public String sayHello() {
        return "Hello from DemoApplication!";
    }
}

Any help is appreciated.

syydi
  • 119
  • 2
  • 13
  • Does this answer your question? [Servlet returns "HTTP Status 404 The requested resource (/servlet) is not available"](https://stackoverflow.com/questions/11731377/servlet-returns-http-status-404-the-requested-resource-servlet-is-not-availa) – Piotr P. Karwasz May 15 '21 at 12:22
  • 1
    More precisely: Tomcat 10.0 is a Jakarta EE 9 servlet container, while Spring Web only works with Java EE 8. Downgrade to Tomcat 9.0 and you should be fine. – Piotr P. Karwasz May 15 '21 at 12:24
  • @PiotrP.Karwasz This helped! Thank you. I switched to Tomcat 9. – syydi May 15 '21 at 14:09
  • @PiotrP.Karwasz I cannot see a suggestion to downgrade Tomcat version in the answers of the post provided in the first comment. I won't mark this question having "a similar question that may solve your problem" as I believe the second comment was the solution for me. – syydi May 15 '21 at 14:32
  • Yes, you are right, the link is not direct, although there is a section _"`javax.servlet.*` doesn't work anymore in Servlet 5.0 or newer"_ in BalusC answer. – Piotr P. Karwasz May 15 '21 at 14:35
  • The question [Why is upgrading to Tomcat 10.0.5 causing spring boot to shutdown after boot?](https://stackoverflow.com/q/67525200/11748454) is more closely related to the problem you are having. – Piotr P. Karwasz May 15 '21 at 14:41

1 Answers1

0

Either use @GetMapping (usually at the controller method level but for this i guess the class would do too) or use RequestMapping(method=RequestMethod.GET).

 @GetMapping("/")
    public String sayHello() {
        return "Hello from DemoApplication!";
    }

OR

 @RequestMapping(method=RequestMethod.GET, "/")
    public String sayHello() {
        return "Hello from DemoApplication!";
    }

Beyond that Check your Tomcat logs (access log for the request manager/catalina to look for a proper deployment) also whats your Java version (also check you compiler compliance) check this https://tomcat.apache.org/whichversion.html.

Bender
  • 72
  • 6