2

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
Mark Rotteveel
  • 100,966
  • 191
  • 140
  • 197
reddynr
  • 21
  • 1
  • 4
  • 1
    Try using `localhost:8080/sample/hello` – S B Mar 16 '20 at 03:34
  • when i access this on embed server it works but on external tomcat it is not working – reddynr Mar 16 '20 at 12:11
  • Does this answer your question? [Servlet 5.0 JAR throws compile error on javax.servlet.\* but Servlet 4.0 JAR does not](https://stackoverflow.com/questions/64387472/servlet-5-0-jar-throws-compile-error-on-javax-servlet-but-servlet-4-0-jar-does) – Piotr P. Karwasz Apr 19 '21 at 01:17
  • 3
    As far as I'm aware you cannot run Spring Boot on Tomcat 10, as Tomcat 10 uses the new Jakarta EE namespace (`jakarta.*`), instead of the old Java EE namespace (`javax.*`), see also https://github.com/spring-projects/spring-boot/issues/22414. So don't try to run Spring Boot on Tomcat 10, use Tomcat 9 or 8.5. – Mark Rotteveel Apr 19 '21 at 07:55
  • After adding firewall rule for selected port on windows machine works fine – Somnath Kadam Aug 11 '23 at 14:41

4 Answers4

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.

Procrastinator
  • 2,526
  • 30
  • 27
  • 36
Pavan
  • 31
  • 3
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
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

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>
dasunse
  • 2,839
  • 1
  • 14
  • 32