运行ng serve时将angular嵌入到spring应用程序中并访问Spring Controllers

时间:2018-11-19 10:27:46

标签: java spring angular maven spring-boot

我计划建立一个Spring-Angular应用程序。我从一个Hello World示例开始,测试了如何设置环境。我最终要做的是:

创建一个Spring项目并在此应用程序中创建一个Angular应用程序。现在,我可以通过Spring-REST-Controllers Angular模块访问HttpClient。 (代码示例如下)。

优点:我可以使用mvn包将Angular和Spring部件打包到一个jar中,然后将其部署在tomcat上。可悲的是,当我运行ng serve时,只会执行前端,而无法访问后端中的数据。有没有办法设置我的环境,以便我可以享受一个项目解决方案的优势,并且仍然可以使用ng serve进行测试?

我尝试过的事情:

打包jar并通过终端(java -jar %jar-file%)执行,然后将localhost:8080/hello用作我的HttpClient的路径,而不是简单的/hello。这并没有令人遗憾地解决。

到目前为止我得到的代码:

app.component.ts

import { Component, OnInit } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
  title = 'HelloWorld';
  message: string;

  constructor(private http : HttpClient) {}

  ngOnInit() : void {
    //this is where I tried to use localhost:8080/hello instead
    this.http.get('/hello').subscribe( data => {
      console.log('DATA', data);
      this.message = data['message'];
    });
  }
}

Rest-Controller:

package com.example.helloWorld.controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;


@RestController
public class HelloWorldController {

    @GetMapping("/hello")
    public String sayHello() {
        return "{\"message\": \"Hello, World!\"}";
    }

}

pom.xml http://maven.apache.org/xsd/maven-4.0.0.xsd“>     4.0.0

<groupId>com.example</groupId>
<artifactId>helloWorld</artifactId>
<version>0.0.1-SNAPSHOT</version>
<packaging>jar</packaging>

<name>helloWorld</name>
<description>Demo project for Spring Boot</description>

<parent>
    <groupId>org.springframework.boot</groupId>
    <artifactId>spring-boot-starter-parent</artifactId>
    <version>2.1.0.RELEASE</version>
    <relativePath/> <!-- lookup parent from repository -->
</parent>

<properties>
    <project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
    <project.reporting.outputEncoding>UTF-8</project.reporting.outputEncoding>
    <java.version>1.8</java.version>
</properties>

<dependencies>

    <dependency>
        <groupId>org.springframework.boot</groupId>
        <artifactId>spring-boot-starter-test</artifactId>
        <scope>test</scope>
    </dependency>
    <dependency>
        <groupId>org.springframework.restdocs</groupId>
        <artifactId>spring-restdocs-mockmvc</artifactId>
        <scope>test</scope>
    </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-autoconfigure</artifactId>
  <version>2.1.0.RELEASE</version>
  <type>jar</type>
 </dependency>
 <dependency>
  <groupId>org.springframework.boot</groupId>
  <artifactId>spring-boot-starter-web</artifactId>
 </dependency>
</dependencies>

<build>
    <plugins>
                    <plugin>
                        <groupId>org.codehaus.mojo</groupId>
                        <artifactId>exec-maven-plugin</artifactId>
                        <executions>
                            <execution>
                                <phase>validate</phase>
                                <goals>
                                    <goal>exec</goal>
                                </goals>
                            </execution>
                        </executions>
                        <configuration>
                            <executable>ng</executable>
                            <workingDirectory>src/main/ui</workingDirectory>
                            <arguments>
                                <argument>build</argument>
                            </arguments>
                        </configuration>
                    </plugin>
        <plugin>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-maven-plugin</artifactId>
        </plugin>
    </plugins>
</build>

1 个答案:

答案 0 :(得分:3)

要这样做

this.http.get('/hello').subscribe( data => {
      console.log('DATA', data);
      this.message = data['message'];
    });

您需要进行一些代理配置。 在项目内的proxy-config.json所在的同一目录中创建一个package.json文件。

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

,并在package.json内的scripts中,用"start"更新"start": "ng serve --proxy-config proxy-config.json",命令 之后,尝试使用npm start命令运行您的项目。