首页 > 解决方案 > 同一服务器上的两个项目具有不同的端口但客户端(Angular 6)无法调用服务器(Spring Boot)

问题描述

更新

我在一台服务器上放置了两个项目,它们是客户端和服务器 RestAPI 应用程序。我将 4200 设置为客户端的端口(由 Angular 6 编写)和服务器端 WebSrv 的端口 8990。我使用命令运行 Spring one java -jar artifact.jar,它工作正常并响应任何请求。但是对于客户端,当我使用 IP:localhost:4200(使用 IntellijIdea 内置构建器或使用 Angular CLI)从本地计算机运行它时,它可以正常工作并且可以发送请求并接收响应。但是,当我从该服务器(WebSrv 服务器所在的同一位置)运行它并运行时,它会正确显示第一个 html 页面但是当我单击发送请求的按钮时,什么也没有发生(也没有异常或任何日志)并且服务器不是收到任何请求!!

我已经搜索了我的问题,但没有任何帮助。如果有人能帮助我找到解决方案,我将不胜感激。我想知道是否有人知道是什么问题。

这是我的客户端代码(Angular)

import {Component, NgModule, OnInit} from '@angular/core';
import {HttpClient, HttpHeaders} from '@angular/common/http';
import {RouterModule, Router} from '@angular/router';

// const URL = 'http://localhost:8990/getUserId';
const URL = 'http://getuserid.mycompany.com:8990/getUserId';


@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent {

  constructor(
    private http: HttpClient
  ) {
  }

  fileToUpload: File = null;
  id: String = '0';

  inputId(event) {
    this.id = event.target.value;
    console.log('id is -- > ' + event.target.value);
  }

  inputFile(event) {
    this.fileToUpload = event.target.files[0];
    console.log('File path -- > ' + event.target.files[0].name);
  }

  onSubmit(id: string, file: File) {
    const frmData = new FormData();
    console.log('POST');
    // @ts-ignore
    frmData.append('id', this.id);
    frmData.append('inputPackage', this.fileToUpload);
    console.log('id --> ' + this.id);
    console.log('File name --> ' + this.fileToUpload.name); 
    this.http.post(URL, frmData).subscribe(res => {
      const resp = JSON.parse(JSON.stringify(res));
      if (resp['user'] != null) {
          if (resp['user']['user-id'] === false) { 
            alert('Successful!! Your User ID is : ' + resp['user']['user-id']); 
          } else {
            alert('Sorry!! Error occurred : ' + resp['error-message']);
          }
     } else {
        alert('Sorry!! Error occurred : ' + resp['error-message'] );
      }
    });

  }
}

这是我的服务器端代码(Spring Boot):

import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;



@Controller
public class GetUserIdController {
    private final static String DESTINATION_PATH = "/srv/resources/";
//    private final static String DESTINATION_PATH = "/mnt/d/DestPath/";
//    private final static String DESTINATION_PATH = "C:/Resources/Temp/";

    @CrossOrigin(origins = "http://localhost:4200", maxAge = 3600)
    @RequestMapping(method = RequestMethod.POST, value = "/getUserId",  headers = {"content-type=multipart/mixed","content-type=multipart/form-data"})
    @ResponseBody
    String Response(@RequestParam("inputPackage") MultipartFile[] inputPackages, @RequestParam("id") String id) {

        String response = null;
        String id ; 
        try {

            if (inputPackages != null && id != null && inputPackages.length > 0 && id.length() > 1) {

                ReceivedPackage recvPackage = new ReceivedPackage();
                recvPackage.setPId(id);
                if (inputPackages[0].getOriginalFilename() != null ) {
                    if( inputPackages[0].getOriginalFilename().contains(".zip")) {
                         FileUtils.saveFile(inputPackages[0].getInputStream(),inputPackages[0].getOriginalFilename(), DESTINATION_PATH);
                        recvPackage.setPackageName(inputPackages[0].getOriginalFilename());
                        recvPackage.setPackagePath(DESTINATION_PATH);  
                        recvPackage.setInputPackage(new File ( recvPackage.getPackagePath()));

                        response = GetUserId.runProcess(recvPackage, DESTINATION_PATH, id); 

                    }else{

                        response = "<error-message>The input file : "+ (inputPackages[0].getOriginalFilename())+" is invalid!!\n It should be a zip file!</error-message>";
                    }
                }
            }else{

                response = "<error-message>The ID and valid zip file should be provide!</error-message>" ;
            }
        } catch (IOException e) {
            e.printStackTrace();
        }

        return FileUtils.getJSONFormat(response);
    }
}

提前致谢。

标签: javarestspring-bootserverangular6

解决方案


4200问题是公司网络部过滤的端口号。更改端口后,8080它运行良好。


推荐阅读