尝试将请求 /api/v1/mega/building 从 localhost:4200 代理到 http://localhost:8080 时发生错误(ECONNREFUSED)

分享于2022年07月17日 angular java node.js spring 问答
【问题标题】:尝试将请求 /api/v1/mega/building 从 localhost:4200 代理到 http://localhost:8080 时发生错误(ECONNREFUSED)(Error occurred while trying to proxy request /api/v1/mega/building from localhost:4200 to http://localhost:8080 (ECONNREFUSED))
【发布时间】:2022-01-26 15:26:17
【问题描述】:

这是我第一次使用 Java 后端和 Angular 前端。 我正在尝试构建和运行这个项目,但我遇到了这个错误: “[HPM] 尝试将请求 /api/v1/mega/building 从 localhost:4200 代理到 http://localhost:8080 (ECONNREFUSED) 时出错”

我运行 .\gradlew.bat build 来构建 Java 应用程序,并在角度和节点 js 代码上运行 npm install 和 npm run start..

我是否必须在构建项目后以某种方式启动 Java 服务器?

这是我的代码:

main.java


import com.nortal.mega.persistence.BuildingRepository;
import com.nortal.mega.persistence.entity.BuildingDbo;
import lombok.AllArgsConstructor;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;
import org.springframework.boot.context.event.ApplicationReadyEvent;
import org.springframework.context.event.EventListener;

import java.util.Arrays;

@AllArgsConstructor
@SpringBootApplication
public class Main {

    private final BuildingRepository buildingRepository;

    public static void main(String[] args) {
        SpringApplication.run(Main.class, args);
    }

proxy.conf.json 我试过把目标改成127.0.0.1,[::]都无济于事

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


【解决方案1】:

听起来您的后端只是没有运行。启动 Spring Boot 应用程序:

./gradlew bootRun

从您的 java 应用程序的根目录

编辑: 看起来你在 Windows 上,所以它会是这样的

.\gradlew.bat bootRun

  • 非常感谢,它成功了!
  • 您有任何关于 Java Spring 和 Angular 的文档、pdf 或视频可以推荐给我吗?
  • 我对 Angular 了解不多。对于我在 Spring Boot 中遇到的任何问题,这个网站: baeldung.com/spring-boot 通常都有高质量的教程。 Spring的文档也很透彻,如果你想在看完教程后了解更多细节,可以阅读官方文档。
  • 好的,谢谢,我去看看