【发布时间】:2020-07-17 13:09:43
【问题描述】:
灵感来源:
https://github.com/jonashackt/spring-boot-vuejs
我正在使用 frontend-maven-plugin 构建一个 vue.js 前端 和一个 spring-boot 后端。我的项目结构如下:
webapp
-> webapp-backend
-> webapp-frontend
在开发过程中运行npm run serve 工作正常,我可以访问我的前端:
但是当我构建应用程序(使用 frontend-maven-plugin)并运行它时:
mvn clean install
java -jar webapp-backend/target/webapp-backend-1.0.0-SNAPSHOT.jar
我只是得到一个空白页:
即使 java 日志中没有错误。
我需要应用一些额外的配置吗? spring-boot 后端让它在生产构建期间正确重定向到我的前端?
下面是更多细节:
webapp/webapp-backend/src/main/java/hello/GreetingController.java
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
@RestController
@RequestMapping("/api")
public class GreetingController {
@RequestMapping(value = "/**/{[path:[^\\.]*}")
public String redirect() {
// Forward to home page so that route is preserved.
return "forward:/";
}
}
webapp-backend/pom.xml
<build>
<plugins>
...
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>Copy Vue.js frontend assets</id>
<phase>generate-resources</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>src/main/resources/public</outputDirectory>
<overwrite>true</overwrite>
<resources>
<resource>
<directory>${project.parent.basedir}/webapp-frontend/dist</directory>
<includes>
<include>static/</include>
<include>index.html</include>
</includes>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
webapp-backend/src/main/resources/public/index.html(非空 index.html 文件)
webapp/webapp-frontend/pom.xml
...
<build>
<plugins>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>${frontend-maven-plugin.version}</version>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
<configuration>
<nodeVersion>v10.0.0</nodeVersion>
</configuration>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>install</arguments>
</configuration>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
...
【问题讨论】:
-
第一个问题:我觉得你的 java 命令行运行 jar 很奇怪:
webapp-backend/...。在我看来,您没有任何 webapp-backend 文件夹?你能告诉我你的 application.properties 的内容是什么吗?以及您的文件夹的内容:backend/src/main/resources/public?谢谢 -
这是一个错字我已经更新了我的帖子。
-
和
webapp-backend/src/main/resources/public文件夹的内容? -
查看更新后的帖子,其中包含:
webapp-backend/src/main/resources/public/index.html
标签: java spring-boot maven vue.js