【问题标题】:Springboot app web page returns 404 when deployed to production部署到生产环境时,Spring Boot 应用程序网页返回 404
【发布时间】:2018-01-17 14:33:52
【问题描述】:

我有一个 springboot 应用程序,在本地运行时可以很好地为网页提供服务,但由于某种原因,当我将部署 war 文件打包到生产环境时返回 404,我收到 404 错误。在我的控制器中,我返回一个 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 应用的主类

@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);
 }
}

我的扩展 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个多小时了。 谢谢。

【问题讨论】:

  • 您要访问的网址是什么?
  • 类似 111.###.###.###:8082/demo(战争文件的名称)
  • 尝试将index.html放到webapp下
  • 嘿。这实际上有效。如果我想组织我的 html 页面,我只需将它们放在 webapp 下的静态文件夹中?

标签: java maven spring-mvc spring-boot tomcat7


【解决方案1】:

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

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 如果链接失效,您的答案应该能够独立存在。如果您提供基本大纲和更多信息的链接,这就足够了,但仅提供链接的答案是不受欢迎的。
    猜你喜欢
    • 2016-12-03
    • 1970-01-01
    • 1970-01-01
    • 2016-10-11
    • 2023-04-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-04-02
    相关资源
    最近更新 更多