GET http://localhost:4200/resource 404(未找到)

GET http://localhost:4200/resource 404 (Not Found)

我正在尝试从 Spring 引导应用程序获取数据到 Angular 应用程序。

我的 Angular 组件具有以下代码:

constructor(private http: HttpClient) { 
    http.get('resource').subscribe(data => this.greeting = data);
}

我的 Spring 引导控制器具有以下代码:

@RequestMapping("/resource")
public Map<String,Object> home() {
    Map<String,Object> model = new HashMap<String,Object>();
    model.put("id", UUID.randomUUID().toString());
    model.put("content", "Hello World");
    return model;
}

但是,当我 运行 这个应用程序时,我收到 404 错误,如下图所示:

您应该请求引导您的服务器的域。 在这种情况下,您正在向 localhost:4200 发出 GET 请求,这是默认的 Angular CLI 域+端口。

所以您正在向 Front-end 应用程序所在的同一端口发出请求。

如果您的服务器在 localhost:3000 上,您可以像这样执行获取请求:

`this.http.get(`http://localhost:3000/resources`).subscribe()...`