与前端和后端的错误通信(Java - Angular)

时间:2017-09-11 16:41:46

标签: node.js angular spring-boot angular-http

我尝试用我的Java / Spring Boot后端传达我的Angular前端。但是终端向我显示了这个错误:

[HPM]尝试将请求/ api / dados从localhost:4500代理到http:/ localhost:8080(ECONNREFUSED)(https://nodejs.org/api/errors.html#errors_common_system_errors

时发生错误

这是我的exemple.service.ts(Angular)代码。我在这里请求数据:

import {Injectable} from "@angular/core";
import {Http, Response} from "@angular/http";
import 'rxjs/Rx';

@Injectable()
export class ExService{
  constructor(private http: Http){

  }

  getName(){
    return this.http.get("/api/dados").map(
      (response: Response) =>{
        return response.json();
      }
    );
  }
}

这是我的exemple.java(Java代码)。它负责发送数据:

package com.lucas.bef.Envia;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
@RequestMapping("/api/dados")
public class Dados {
    String teste;

    public Dados(){
        this.teste = "LUCAS";
    }

    public void setNome(String nome){
        this.teste = nome;
    }

    @GetMapping( value = {"","/"})
    public String getNome(){
        return teste;
    }
}

如果我启动Spring应用程序......终端显示一条消息Tomcat在8080端口启动。

我创建了一个配置代理文件(proxy-conf.json),如下所示:

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

请有人帮助我。我需要这样做。非常重要。谢谢!

2 个答案:

答案 0 :(得分:1)

问题似乎在于您运行这两个应用程序(前端 和后端)在不同的端口上。这样的请求称为CORS。您可以在Spring-Boot App中启用cors,如this。或者,对于临时测试,请下载此plugin

答案 1 :(得分:0)

首先你已经设置了“目标”:“http:/ localhost:8080”,错了!它是http://localhost:8080

另请尝试重写来自此服务的请求

 getName(){
    return this.http.get("http://localhost:8080/api/dados").map(
      (response: Response) =>{
        return response.json();
      }
    );
  }

检查它是否正常工作!您必须从服务方法

订阅Observable响应

此外,您必须允许Spring服务器与角度应用程序通信。为此,您必须从后端配置CORS

import java.io.IOException;
import javax.servlet.FilterChain;
import javax.servlet.ServletException;
import javax.servlet.ServletRequest;
import javax.servlet.ServletResponse;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.web.filter.GenericFilterBean;


public class CORSFilter extends GenericFilterBean {

  @Override
  public void doFilter(ServletRequest req, ServletResponse res, FilterChain chain)
      throws IOException, ServletException {

    if (((HttpServletRequest) req).getMethod().equals("OPTIONS")) {

      HttpServletResponse response = (HttpServletResponse) res;
      response.setHeader("Access-Control-Allow-Origin", "http://localhost:4500");
      response.setHeader("Access-Control-Allow-Credentials", "true");
      response.setHeader("Access-Control-Allow-Methods", "GET,HEAD,OPTIONS,POST,PUT,DELETE");
      response.setHeader("Access-Control-Allow-Headers", "Access-Control-Allow-Headers, Origin,Accept, "
            + "X-Requested-With, Content-Type, Access-Control-Request-Method, Access-Control-Request-Headers");

    } else {

      chain.doFilter(req, res);

    }
  }
}