0
votes

I am trying deploy my spring application in tomcat 9 with a war file, I can generate the .war file but I get an error 404. My application is extending of SpringBootServletInitializer and I overrided the configure method, my pom.xml file has the dependency "spring-boot-starter-tomcat" too.

There aren't any error in logs. But if I put an index.html in the directory of my webapps tomcat loads it. I need tomcat loads the index.jsp but it was generated in WEB-INF/views.

The tree (-L 2) of the deploy directory:

.
├── META-INF
│   ├── MANIFEST.MF
│   ├── maven
│   └── war-tracker
├── org
│   └── springframework
└── WEB-INF
    ├── classes
    ├── lib
    ├── lib-provided
    └── views

Source structure

.
├── java
│   └── com
│       └── company
│           └── app
│               ├── controller
│               ├── Application.java
│               ├── model
│               ├── service
│               └── util
├── resources
│   └── application.properties
└── webapp
    └── WEB-INF
        └── views
            ├── secondary.jsp
            └── index.jsp

My pom.xml file:

<?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 https://maven.apache.org/xsd/maven-4.0.0.xsd">
    <modelVersion>4.0.0</modelVersion>

    <!-- ... -->
    <packaging>war</packaging>
    <!-- ... -->

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

    <groupId>com.netexlearning</groupId>
    <artifactId>convalidator</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>convalidator</name>
    <description>Spring Boot</description>

    <properties>
        <java.version>11</java.version>
        <start-class>com.company.app.Application</start-class>
        <servlet-api.version>3.1.0</servlet-api.version>
    </properties>

    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-devtools</artifactId>
            <scope>runtime</scope>
            <optional>true</optional>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-tomcat</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>commons-fileupload</groupId>
            <artifactId>commons-fileupload</artifactId>
            <version>1.3.3</version>
        </dependency>
        <dependency>
            <groupId>org.json</groupId>
            <artifactId>json</artifactId>
            <version>20190722</version>
        </dependency>
        <dependency>
            <groupId>javax.servlet</groupId>
            <artifactId>jstl</artifactId>
            <version>1.2</version>
        </dependency>
        <dependency>
            <groupId>org.apache.tomcat.embed</groupId>
            <artifactId>tomcat-embed-jasper</artifactId>
            <scope>provided</scope>
        </dependency>
        <dependency>
            <groupId>org.eclipse.jdt.core.compiler</groupId>
            <artifactId>ecj</artifactId>
            <version>4.6.1</version>
            <scope>provided</scope>
        </dependency>
    </dependencies>

    <build>
        <finalName>${project.artifactId}</finalName>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

application.properties:

path.file.service=uploaded
spring.servlet.multipart.max-file-size=2MB
spring.servlet.multipart.max-request-size=2MB
spring.mvc.view.prefix=/WEB-INF/views/

spring.mvc.view.suffix=.jsp

Aplication.java

@SpringBootApplication
@EnableConfigurationProperties(StorageProperties.class)
public class Application extends SpringBootServletInitializer {

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

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

    @Bean
    CommandLineRunner init(StorageService storageService) {
        return (args) -> {
            storageService.deleteAll();
            storageService.init();
        };
    }

}

Tell me if you need more information.

1
How does your source structure look like? - Simon Martinelli
which tomcat version and spring boot version you're working with? - Matheus Cirillo
my pom.xml file has the dependency "spring-boot-starter-tomcat" too... , note scope should be provided. ref: howto-create-a-deployable-war-file - Dirk Deyne

1 Answers

0
votes

I solved it.

I was using a storage service, it need permisions for write but I wasn't sure where, then I make a mistake: I used chmod 664 in the deployed folder in webapps, Tomcat "run" my project but always the response was 404 error. Spring never executed anything. I removed the folder in webapps, change the propertie path.file.service=uploaded to path.file.service=/tmp/file and I generated the war file again.

It's working now.