我已经使用带有 React 前端的 springboot 构建了一个 Web 应用程序,当我在本地运行该应用程序时,我可以毫无问题地使用我的 REST API 调用。一旦我将所有内容打包成一个 war 文件并将其部署到我的 VM 上的 tomcat 服务器上,我就无法再访问我的后端了。
catalina.out 日志文件中没有出现错误,但也没有 springboot 应用程序启动的迹象。这就是我得到的全部。
17-Jul-2024 19:20:56.108 INFO [http-nio-0.0.0.0-8080-exec-2] org.apache.catalina.startup.HostConfig.deployWAR Deploying web application archive [/opt/tomcat/webapps/record-retention.war]
17-Jul-2024 19:20:56.305 WARNING [http-nio-0.0.0.0-8080-exec-2] org.apache.tomcat.util.descriptor.web.WebXml.setVersion Unknown version string [5.0]. Default version will be used.
17-Jul-2024 19:20:58.396 INFO [http-nio-0.0.0.0-8080-exec-2] org.apache.jasper.servlet.TldScanner.scanJars At least one JAR was scanned for TLDs yet contained no TLDs. Enable debug logging for this logger for a complete list of JARs that werening can improve startup time and JSP compilation time.
17-Jul-2024 19:20:58.416 INFO [http-nio-0.0.0.0-8080-exec-2] org.apache.catalina.startup.HostConfig.deployWAR Deployment of web application archive [/opt/tomcat/webapps/record-retention.war] has finished in [2,308] ms
这是我的应用程序类。
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.builder.SpringApplicationBuilder;
import org.springframework.boot.web.servlet.support.SpringBootServletInitializer;
@SpringBootApplication
public class App extends SpringBootServletInitializer{
public static void main(String[] args) {
SpringApplication.run(App.class, args);
}
@Override
protected SpringApplicationBuilder configure(SpringApplicationBuilder application){
return application.sources(App.class);
}
}
我的 pom.xml 的构建部分
<build>
<plugins>
<plugin>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-maven-plugin</artifactId>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.12.1</version>
<executions>
<execution>
<id>Install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v20.14.0</nodeVersion>
<npmVersion>10.8.1</npmVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>install</arguments>
<workingDirectory>src/main/record-retention-ui</workingDirectory>
</configuration>
</execution>
<execution>
<id>npm build</id>
<goals>
<goal>npm</goal>
</goals>
<phase>generate-resources</phase>
<configuration>
<arguments>run build</arguments>
<workingDirectory>src/main/record-retention-ui</workingDirectory>
</configuration>
</execution>
</executions>
<configuration>
<workingDirectory>src/main/record-retention-ui</workingDirectory>
</configuration>
</plugin>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>Copy JavaScript app into Springboot</id>
<phase>process-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${project.build.directory}/${project.build.finalName}/</outputDirectory>
<resources>
<resource>
<directory>src/main/record-retention-ui/build</directory>
<filtering>false</filtering>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
在发帖之前,我已经解决这个问题两天了,但没有效果,我实现了 servletInitializer,因为我最初没有。我通过服务器访问前端没有问题,只是无法进行任何 API 调用。
这是我第一次尝试将网络应用程序部署到服务器,因此非常感谢任何帮助/建议。如果需要更多信息我可以提供!