部署到生产环境时,Spring Boot 应用网页 returns 404

Springboot app web page returns 404 when deployed to production

我有一个 springboot 应用程序,当 运行 在本地时,它可以很好地提供网页,但由于某种原因 returns 404 当我将 war 文件打包部署到生产环境时,我收到 404 错误。在我的控制器中,我 return 一个 jsp 页面,其中包含来自我的 /resources/static 文件夹的 html 页面。这一切在本地都可以正常工作。

我正在部署到 tomcat 7.0.52

这是我的pom

<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>demo</groupId>
<artifactId>demo</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>war</packaging>

<properties>
    <spring.version>4.3.9.RELEASE</spring.version>
    <java.version>1.8</java.version>
    <maven-compiler.version>3.6.1</maven-compiler.version>
</properties>
<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>1.5.4.RELEASE</version>
    <relativePath /> <!-- lookup parent from repository -->
</parent>
<dependencies>
    <!-- //////////////////////////////////////////////////////// -->
    <!-- SPRING -->
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-context</artifactId>
    </dependency>
    <dependency>
        <groupId>org.springframework</groupId>
        <artifactId>spring-webmvc</artifactId>
    </dependency>

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

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-tomcat</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat</groupId>
        <artifactId>tomcat-jdbc</artifactId>
        <version>7.0.19</version>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>org.apache.tomcat.embed</groupId>
        <artifactId>tomcat-embed-jasper</artifactId>
        <scope>provided</scope>
    </dependency>
    <dependency>
        <groupId>javax.servlet</groupId>
        <artifactId>jstl</artifactId>
    </dependency>
</dependencies>

<build>
    <plugins>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
            <executions>
                <execution>
                    <goals>
                        <goal>
                            repackage
                        </goal>
                    </goals>
                </execution>
            </executions>
        </plugin>
        <plugin>
            <groupId>org.apache.tomcat.maven</groupId>
            <artifactId>tomcat7-maven-plugin</artifactId>
            <version>2.2</version>
        </plugin>
        <plugin>
            <groupId>org.apache.maven.plugins</groupId>
            <artifactId>maven-compiler-plugin</artifactId>
            <configuration>
                <source>${java.version}</source>
                <target>${java.version}</target>
            </configuration>
        </plugin>
    </plugins>
</build>

`

我的 Springboot 应用程序的主要 class

@SpringBootApplication
public class SpringbootApplication extends SpringBootServletInitializer {

@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application) {
    return application.sources(SpringbootApplication.class);
}

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

我的 class 扩展了 webmvcConfigureAdapter

@Configuration
@EnableWebMvc
public class MvcConfiguration extends WebMvcConfigurerAdapter{

@Override
public void addViewControllers(ViewControllerRegistry registry) {
    System.out.println("in mvc config");
    registry.addViewController("/").setViewName("forward:index2.html");
}

@Override
public void configureDefaultServletHandling(
        DefaultServletHandlerConfigurer configurer) {
    System.out.println("configure serve handling");
    configurer.enable();
}  

@Bean
InternalResourceViewResolver internalResourceViewResolver () {
    InternalResourceViewResolver viewResolver = new InternalResourceViewResolver();
    viewResolver.setPrefix("/WEB-INF/jsp/");
    viewResolver.setSuffix(".jsp");
    return viewResolver;
}
}

我的主控。

@Controller
public class MainController {
    @RequestMapping(value={"/"}, method = RequestMethod.GET)
    public String index(){
        System.out.println("inside mainController");
        return "chatbot";
    }
}

最后我的 application.properties 文件看起来像这样。

spring.mvc.view.prefix: /WEB-INF/jsp/
spring.mvc.view.suffix: .jsp

我的项目结构如下所示...

src/main/webapp
               -> assets
               -> resources
                   -> static ->index.html
               -> WEB-INF      
                   ->jsp -> index.jsp

请帮忙。我已经坚持这个超过 8 个小时了。 谢谢。

也许问题在于您必须包含应用程序上下文路径。

检查以下 link 以执行此操作 add-context-path-to-spring-boot-application

您可以将 index.html 放在 webapp 下,因为这是默认的上下文路径,所有其他资源都应该与其相关。