代码之家  ›  专栏  ›  技术社区  ›  Ruchi Pareek

部署angular 5 springboot多模块项目-jar

  •  1
  • Ruchi Pareek  · 技术社区  · 7 年前

    我在的帮助下创建了一个springboot多模块项目 tutorial 两个模块-一个后端(java类),另一个前端(angular 5应用程序) 我在后端模块中包含了前端模块的依赖项。 我正在使用maven资源插件创建一个jar。 我正在将静态资源复制到pom中构建目录的静态文件夹中。xml也是。 我还有一个返回“index”的@控制器。 当我运行jar时,我希望看到索引。要在localhost:8080上呈现的html(前端模块)。 但我得到一个内部服务器错误,说“模板解析器找不到索引。 我知道@Contoller从templates文件夹呈现HTML,但在我的情况下,我希望它从frontend模块呈现。

    这是我的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>
    

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

    2 回复  |  直到 7 年前
        1
  •  1
  •   Ruchi Pareek    7 年前

    进行了以下更改,效果良好- 1、从pom中删除了maven资源插件,而只是添加了

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

    当我点击localhost:8080时,我可以看到spring security browser登录弹出窗口/

        2
  •  0
  •   Ahmed Itani    7 年前

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

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