部署 Angular 5 Springboot 多模块项目 -jar



我在教程的帮助下创建了一个 springboot 多模块项目 两个模块 - 一个后端(Java类(,另一个Forntend(Angular 5应用程序( 我在后端模块中包含前端模块的依赖项。 我正在使用 maven 资源插件创建一个罐子。 我也正在将静态资源复制到我的pom中构建目录的静态文件夹中.xml。 我还有一个返回"索引"的@Controller。 当我运行jar时,我希望看到index.html(前端模块(在本地主机:8080上渲染。 但是我收到一个内部服务器错误,说"模板解析器找不到索引。 我知道@Contoller从模板文件夹呈现 HTML,但就我而言,我希望它从前端模块呈现。

这是我的pom.xml前端模块是 -

<build>
<plugins>
<plugin>
<artifactId>maven-resources-plugin</artifactId>
<executions>
<execution>
<id>copy-resources</id>
<phase>validate</phase>
<goals>
<goal>copy-resources</goal>
</goals>
<configuration>
<outputDirectory>${build.directory}/classes/static</outputDirectory>
<resources>
<resource>
<directory>target/frontend</directory>
</resource>
</resources>
</configuration>
</execution>
</executions>
</plugin>
<plugin>
<groupId>com.github.eirslett</groupId>
<artifactId>frontend-maven-plugin</artifactId>
<version>1.6</version>
<configuration>
<nodeVersion>v8.10.0</nodeVersion>
<npmVersion>5.6.0</npmVersion>
<workingDirectory>src/main/frontend</workingDirectory>
</configuration>
<executions>
<execution>
<id>install node and npm</id>
<goals>
<goal>install-node-and-npm</goal>
</goals>
</execution>
<execution>
<id>npm install</id>
<goals>
<goal>npm</goal>
</goals>
</execution>
<execution>
<id>npm run build</id>
<goals>
<goal>npm</goal>
</goals>
<configuration>
<arguments>run build</arguments>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
<!-- <resources> -->
<!-- <resource> -->
<!-- <directory>target/frontend</directory> -->
<!-- <targetPath>static</targetPath> -->
<!-- </resource> -->
<!-- </resources> -->
</build>

我是春天的新手,不知道我做错了什么。请帮忙

进行了以下更改,它有效 - 1.从pom中删除了maven资源插件,而只是添加了

<resources>
<resource>
<directory>target/frontend</directory>
<targetPath>static</targetPath>
</resource>
</resources>

当我点击本地主机时,我可以看到弹簧安全浏览器登录弹出窗口:8080/

试试这个,它对我来说很好用。

import java.io.ByteArrayOutputStream;
import java.io.IOException;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource;
import org.springframework.stereotype.Controller;
import org.springframework.util.FileCopyUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;

@Controller
public class TestController {
@RequestMapping(method = RequestMethod.GET)
public void getIndex(HttpServletRequest request, HttpServletResponse response) throws IOException {
String uri = request.getRequestURI();
String accessResource = "/static/index.html";
if(uri.contains(".")) { // if css or jpg or font or others
accessResource = "/static" + uri;
}
Resource resource;
ByteArrayOutputStream bos;
resource = new ClassPathResource(accessResource, TestController.class);
bos = new ByteArrayOutputStream();
FileCopyUtils.copy(resource.getInputStream(), bos);
response.getOutputStream().write(bos.toByteArray());
}
}

最新更新