0

I am new to web application development.

I have been working on developing a rest web service and deploying it locally on apache tomcat server. I am using sts for this.

Now, when I am executing my application from sts (Run as java application). It is running fine. I am hitting the url at localhost:8080 and everything is fine.

Now, I am doing the following :

  1. Created a .war file for my application, named sample-rest-service.war
  2. On a different system, I have downloaded and installed tomcat 8 configured to run on port 9999
  3. I copy my .war file in the webapps folder of apache-tomcat-8.028
  4. I start my tomcat server using startup.sh
  5. I type localhost:9999 in my browser and the home page appears
  6. I type localhost:9999/sample-rest-service, but I get the error message with code 404
  7. I type localhost:9999/examples and it works fine, i.r. content appears
  8. I check the webapps folder, .war file has been extracted both war file and the extracted folder are present there.

What am I missing ?

Normally if I run my application through sts, I do this :

  1. Run as Java Application
  2. Then it asks me to chose, I chose Application-hello, where Application is the class having main() and hello is the package
  3. After that in the console I can read the message that Tomcat started on port(s): 8080 (http)
  4. Now in my browser I type this : http://localhost:8080/greeting
  5. I get the output in JSON

web.xml

<?xml version="1.0" encoding="UTF-8"?>
<web-app xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://xmlns.jcp.org/xml/ns/javaee" xsi:schemaLocation="http://xmlns.jcp.org/xml/ns/javaee http://xmlns.jcp.org/xml/ns/javaee/web-app_3_1.xsd" version="3.1">
  <display-name>sample-rest-service</display-name>
  <welcome-file-list>
    <welcome-file>index.html</welcome-file>
    <welcome-file>index.htm</welcome-file>
    <welcome-file>index.jsp</welcome-file>
    <welcome-file>default.html</welcome-file>
    <welcome-file>default.htm</welcome-file>
    <welcome-file>default.jsp</welcome-file>
  </welcome-file-list>
</web-app>

To generate war I did the following :

  1. Added the maven war plugin in my project
  2. changed the packaging in pom.xml from jar(default) to war
  3. Export war file

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>org.springframework</groupId>
    <artifactId>gs-rest-service</artifactId>
    <version>0.1.0</version>

    <parent>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>1.2.6.RELEASE</version>
    </parent>

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

    <properties>
        <java.version>1.7</java.version>
    </properties>


    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <groupId>org.apache.maven.plugins</groupId>
                <artifactId>maven-war-plugin</artifactId>
                <version>2.6</version>
            </plugin>
        </plugins>
    </build>

    <repositories>
        <repository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </repository>
    </repositories>
    <pluginRepositories>
        <pluginRepository>
            <id>spring-releases</id>
            <url>https://repo.spring.io/libs-release</url>
        </pluginRepository>
    </pluginRepositories>


    <packaging>war</packaging>
</project>
11
  • 1
    What appears in your Tomcat access log? Oct 20, 2015 at 7:47
  • @TimBiegeleisen there are many log files inside the logs folder - catalina.date.log, catalina.out, host-manager.log, manager.log, localhost.log, localhost-access.log. Which one is the relevant one ?
    – Ankit
    Oct 20, 2015 at 9:04
  • What does localhost-access.log say? Oct 20, 2015 at 9:08
  • 127.0.0.1 - - [20/Oct/2015:14:36:10 +0530] "GET /sample-rest-service HTTP/1.1" 302 - 127.0.0.1 - - [20/Oct/2015:14:36:10 +0530] "GET /sample-rest-service/ HTTP/1.1" 404 1034 127.0.0.1 - - [20/Oct/2015:14:36:38 +0530] "GET /sample-rest-service/ HTTP/1.1" 404 1034
    – Ankit
    Oct 20, 2015 at 9:11
  • 1
    The reason I suggest a tutorial is that the main purpose of using a Java WAR is the ability to run servlets on your server. You don't even have any of these, so the question comes off as being a bit weird. Oct 20, 2015 at 9:46

1 Answer 1

2

It turns out that the whole problem was with building the war file.

The proper way to build a deployable and runnable war file through sts is explained here

  1. Extend your Application class with SpringBootServletInitializer, and override the configure method

    @SpringBootApplication public class Application extends SpringBootServletInitializer{

    public static void main(String[] args) {
        SpringApplication.run(Application.class, args);
    }
    
    @Override
    protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
        return application.sources(Application.class);
    }
    
  2. In pom.xml, mention war inside packaging tags

  3. you need to mark the spring-boot-starter-tomcat dependencies as “provided”

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.