6

I am trying to deploy a rest sample in external tomcat. However, it does not give any error and Spring does not start also. According to Spring documentation I followed three steps

  1. Packages as war inside pom.xml
  2. <scope>provided</scope> to spring-boot-starter-tomcat in pom.xml
  3. Extending main class with SpringBootServletInitializer

When I place this in Tomcat's webapp folder and run "catalina.bat run" it says application deployed successfully, but whatever URL I try to access, it does not execute the rest method

@SpringBootApplication
@RestController
public class HelloWorldApplication extends SpringBootServletInitializer
{
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application)
    {
        return application.sources(HelloWorldApplication.class);
    }
    public static void main(String[] args)
    {
        SpringApplication.run(HelloWorldApplication.class, args);
    }
    @RequestMapping(value = "/hello")
    public String helloWorld()
    {
        return "Hello World, Peter";
    }
}

and in pom.xml

<dependency>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-tomcat</artifactId>
    <scope>provided</scope>
</dependency>

        
<groupId>com.example</groupId>
    <artifactId>HelloWorld</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>HelloWorld</name>
    <description>Demo project for Spring Boot</description>
    <packaging>war</packaging>

and application.properties is like

server.servlet.context-path=/sample

Tomcat logs:

    15-Mar-2020 23:06:46.219 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war]
    15-Mar-2020 23:06:48.102 INFO [main] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that were scanned but no TLDs were found in them. Skipping unneeded JARs during scanning can improve startup time and JSP compilation time.
    15-Mar-2020 23:06:48.409 WARNING [main] org.apache.catalina.util.SessionIdGeneratorBase.createSecureRandom Creation of SecureRandom instance for session ID generation using [SHA1PRNG] took [270] milliseconds.
    15-Mar-2020 23:06:48.440 INFO [main] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\softwares\apache-tomcat-10.0.0-M1\webapps\HelloWorld-0.0.1-SNAPSHOT.war] has finished in [2,221] ms

I tried accessing in below ways.but none of them worked.Could any one points me in right direction?

localhost:8080/HelloWorld-0.0.1-SNAPSHOT
localhost:8080/HelloWorld-0.0.1-SNAPSHOT/hello
localhost:8080/HelloWorld-0.0.1-SNAPSHOT/HelloWorldApplication/helloWorld
7

5 Answers 5

2

Make sure that the java version you set in your pom.xml <java.version>1.8</java.version> and the java version using which the tomcat is running (version of java to which JAVA_HOME env variable is pointing) are same.

Sign up to request clarification or add additional context in comments.

Comments

2

follow this steps it may helps you

  1. Build the war file : go to spring project directory there you do mvn clean install (make sure you have installed maven and set to environmental variable).
  2. Copy war :copy your war file into /webapps folder inside your tomcat folder.
  3. Start server: to start the server go to /bin folder then execute this commands startup.sh for Linux startup.bat for windows.
  4. Hit Url: Once the server started, open your browser the type localhost:{"portNumber"}/{warFileName}/{yourPath} in your case its look like this http://localhost:8080/{warfilename}/hello

Comments

1

Your path is set up in this functions

@RequestMapping(value = "/hello")
public String helloWorld()
{
    return "Hello World, Peter";
}

Which means the only thing you can access is "/hello". Accessing to localhost:8080/HelloWorld-0.0.1-SNAPSHOT will not working, you have to access for localhost:8080/${context-path}/${your-path} in this case localhost:8080/sample/hello

1 Comment

when i access this on embed server it works but on external tomcat it is not working
0

try this way

localhost:8080/app-name/hello

In your 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>it.group.id</groupId>
    <artifactId>artifactId</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>war</packaging>
    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.5.RELEASE</version>
        <relativePath/> <!-- lookup parent fdaotory -->
    </parent>
    <properties>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
        <java.version>1.8</java.version>
    </properties>
    <dependencies>

        <!--For Build War File-->
        <dependency>
             <groupId>org.springframework.boot</groupId>
             <artifactId>spring-boot-starter-tomcat</artifactId>
             <scope>provided</scope>
        </dependency>



    </dependencies>
    <build>
        <!--DEV-->
        <finalName>app-name</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>

            <!--For Build War File-->
            <plugin>
                <artifactId>maven-war-plugin</artifactId>
                <version>3.2.2</version>
                <configuration>
                    <failOnMissingWebXml>false</failOnMissingWebXml>
                </configuration>
            </plugin>

        </plugins>

    </build>

</project>

Comments

0

I have the same issue , where from the catalina log it is displayed that

02-Mar-2025 10:55:06.454 INFO [http-nio-8080-exec-11] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [C:\Users....\webapps\ecom-0.0.1-SNAPSHOT.war] has finished in [8,155] ms

but when i try to access my endpoints , it wont be reachable(Got 404).
I fixed it by upgrading the tomcat which matches my java version (21)

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.