gpt4 book ai didi

java - 将 Angular 嵌入到 Spring 应用程序中并在运行 ng serve 时访问 Spring Controllers

转载 作者:行者123 更新时间:2023-11-30 07:43:33 25 4
gpt4 key购买 nike

我计划设置一个 Spring-Angular 应用程序。我立即开始使用 Hello World 示例来测试如何设置环境。我最终做了什么:

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

优势:我可以使用 mvn package 将 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'];
});
}
}

休息 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.xmlhttp://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>

最佳答案

这样做

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

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

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

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

关于java - 将 Angular 嵌入到 Spring 应用程序中并在运行 ng serve 时访问 Spring Controllers,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53372609/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com