简体   繁体   English

使用Spring Boot构建Angular应用程序

[英]Build Angular application with Spring boot

I want to build an Agular application, but I don't know how to build a project structure. 我想构建一个Agular应用程序,但是我不知道如何构建项目结构。

I have found 2 ways to build the Application. 我发现了两种方法来构建应用程序。 The first way that I chose was to build together Spring boot application and Angular application. 我选择的第一种方法是将Spring Boot应用程序和Angular应用程序一起构建。 The Second way was to build separate projects. 第二种方法是建立单独的项目。 With the first way, I can get access to static resources easily, but I think It is not a good way. 通过第一种方法,我可以轻松访问静态资源,但是我认为这不是一个好方法。 With the second way, It is good because I can use Spring boot application for web and mobile clients. 通过第二种方法,这很好,因为我可以将Spring Boot应用程序用于Web和移动客户端。

Do I need suggestions on how to build spring-agular like projects? 我需要有关如何构建类似于Spring的项目的建议吗? The basic problem is how to use effectively static resources in an angular project? 基本问题是如何在角度项目中有效利用静态资源?

I have a project that uses Spring boot as backend and angular in the front. 我有一个项目使用Spring Boot作为后端,并且在前端使用角度。 For us, it was better to serve angular using the spring boot. 对我们来说,最好使用弹簧靴来提供角度。 But you can do it in another way. 但是您可以用另一种方式来做。

In a brief, spring boot works as an API to make CRUD, authentication, and others tasks we need. 简而言之,spring boot可以作为API来执行CRUD,身份验证以及我们需要的其他任务。 To serve angular in spring, you need to build your project with ng build --prod and copy all files generated here to the static folder in spring. 为了在春季提供角度服务,您需要使用ng build --prod构建项目,并在春季将此处生成的所有文件复制到静态文件夹。 Nonetheless, you need to adjust the routes and make a proxy (I'll show it). 尽管如此,您仍需要调整路由并建立代理(我将向您展示)。

To adjust the routes, you may include the following configuration: 要调整路由,您可以包括以下配置:

@Configuration
public class AngularConfig implements WebMvcConfigurer {

    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {

      registry.addResourceHandler("/**/*")
        .addResourceLocations("classpath:/static/")
        .resourceChain(true)
        .addResolver(new PathResourceResolver() {
            @Override
            protected Resource getResource(String resourcePath,
                Resource location) throws IOException {
                Resource requestedResource = location.createRelative(resourcePath);
                return requestedResource.exists() && requestedResource.isReadable() ? requestedResource
                : new ClassPathResource("/static/index.html");
            }
        });

    }
}

In dev phase, it is very boring to build angular. 在开发阶段,建立角度非常无聊。 So, it's very desired using ng serve (with the spring up, of course). 因此,非常需要使用ng serve (当然要加弹)。 As angular cli serves on localhost:4200 and spring on localhost:8080 , you have to make a proxy between the address: 由于angular cli在localhost:4200而spring在localhost:8080 ,您必须在以下地址之间建立代理:

1) In the project root you must include a file named proxy.conf.json containing: 1)在项目根目录中,必须包含一个名为proxy.conf.json的文件, proxy.conf.json包含:

{
    "/api": {
        "target": "http://localhost:8080",
        "secure": false
    }
}

2) Next, in the file package.json , replace "start": "ng serve" to "start": "ng serve --proxy-config proxy.conf.json" 2)接下来,在文件package.json ,将"start": "ng serve"替换为"start": "ng serve --proxy-config proxy.conf.json"

There you go! 你去! It should works. 它应该工作。

Finally, it's very boring copy and paste the build. 最后,复制和粘贴构建非常无聊。 So, if you're using maven, you can include a rule to make it for you every time you start spring. 因此,如果您使用的是Maven,则可以包含一条规则,以便在每次启动Spring时都可以使用。 Put it in your pom.xml : 把它放在你的pom.xml

<build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
            <plugin>
                <artifactId>maven-resources-plugin</artifactId>
                <executions>
                      <execution>
                          <id>copy-resources</id>
                          <phase>validate</phase>
                          <goals><goal>copy-resources</goal></goals>
                          <configuration>
                              <outputDirectory>${basedir}/src/main/resources/static/</outputDirectory >
                              <resources>
                                  <resource>
                                      <directory>${basedir}/../frontend/dist</directory >
                                  </resource>
                              </resources>
                          </configuration>
                      </execution>
                </executions>
            </plugin>
        </plugins>
    </build>

Of course, you should set the paths according to your project. 当然,您应该根据您的项目设置路径。

TL;DR: you can work with to projects in the dev phase and include the build in the spring for production. TL; DR:您可以在开发阶段使用项目,并在春季将构建包括在内以进行生产。

If you have any further question, let me know! 如果您还有其他疑问,请告诉我! Good luck. 祝好运。

My personal suggestion; 我个人的建议;

JHipster is a development platform to generate, develop and deploy Spring Boot + Angular/React Web applications and Spring microservices. JHipster是一个开发平台,用于生成,开发和部署Spring Boot + Angular / React Web应用程序和Spring微服务。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM