Springboot 应用程序网页在部署到生产环境时返回 404



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

我正在部署到雄猫 7.0.52

这是我的诗歌

<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";
}
}

最后,我的应用程序属性文件看起来像这样。

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 个多小时了。 谢谢。

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

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

检查以下链接以执行将上下文路径添加到 spring-boot-应用程序

相关内容

最新更新