简体   繁体   中英

Spring runs fine on server but not main application

I'm trying to create a new Spring MVC web app project (MVN). I did the whole setup and can "run on server" via Tomcat with Eclipse. The endpoints all work. I can see my jsp pages.

If I however try to run my main application (SpringApplication.run()) on the otherhand- it won't work. I get a 404 on every page.

@SpringBootApplication
public class App extends SpringBootServletInitializer {
    public static void main(String[] args) {
        SpringApplication.run(App.class,  args);
    }   
}

Why is it that if I run this driver class (as opposed to selecting my folder and running it on the server) I am unable to view any pages and only see 404 white label error pages? How do I fix this?

Edit: It seems like my project setup might have been screwy. I assume it might have been from creating a maven project, converting to dynamic web project, and my dependencies. Ended up using Spring.io rather than trying to configure it myself and things work out fine.

Your current configuration is ment to run on server (extends SpringBootServletInitializer). Dependent on which project management tool you are using (Maven, Gradle) this configuration should after compilation create WAR file which is supposed to be run on application server (or servlet container, depending on your dependencies).

If you wish to run your spring application from command line remove ServletInitializer and with spring boot tomcat dependency (spring-boot-starter-web already contains it) your application should be compiled to JAR file (again you need to specify this in your project management tool) and should run with embedded tomcat servlet container, thus can be run from command line with java -jar command. Please provide more information (dependencies, and configuration) as its not absolutely clear from your question what you are trying to achieve.

EDIT ! to comment section below
Main class:

    @SpringBootApplication
    public class ApplicationLauncher {

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

    @RestController
    public class Root {
        @GetMapping("/")
        public ResponseEntity root() { return ResponseEntity.ok().build(); }
    }
}

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

    <parent> <!-- main spring boot dependency -->
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-parent</artifactId>
        <version>2.1.8.RELEASE</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>

    <groupId>org.example</groupId>
    <artifactId>temp2</artifactId>
    <version>1.0-SNAPSHOT</version>
    <packaging>jar</packaging>

    <properties>
        <!-- global versions -->
        <java.version>11</java.version>
        <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
        <project.build.resourceEncoding>UTF-8</project.build.resourceEncoding>
        <maven.compiler.source>${java.version}</maven.compiler.source>
        <maven.compiler.target>${java.version}</maven.compiler.target>
    </properties>

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

    <build>
        <finalName>application</finalName>
        <plugins>
            <plugin> <!-- plugin to create über WAR file containing all dependencies -->
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <addResources>true</addResources>
                </configuration>
            </plugin>
        </plugins>
    </build>
</project>

You can compile with "mvn clean install" then inside target folder you will find your JAR file which can be run from command line.

Please Check your Pom.xml if it's a maven project.

 <groupId>com.spring.demo</groupId>
 <artifactId>springdemo</artifactId>
 <version>1.0</version>
 <packaging>war</packaging>

Try Removing packaging tag or change War to Jar. so that you can run the project through the main application.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM