简体   繁体   English

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

[英]Two projects on same server with different ports but client (Angular 6) can't call server (Spring Boot)

Update 更新

I've put two project on one server which are client and server RestAPI applications. 我将两个项目放在一台服务器上,分别是客户端和服务器RestAPI应用程序。 I set 4200 as a port for the Client (which is written by Angular 6) and port 8990 for Server side WebSrv. 我将4200设置为客户端的端口(由Angular 6编写),将8990设置为服务器端WebSrv的端口。 I run Spring one with the command java -jar artifact.jar , it works fine and response any requests. 我使用命令java -jar artifact.jar运行Spring一,它可以正常工作并响应任何请求。 But for Client side when I run it from my local machine with IP : localhost:4200 ( either with IntellijIdea built-in builder or with Angular CLI) it works fine and can send the request and receive response. 但是对于客户端,当我从本地计算机使用IP:localhost:4200(使用IntellijIdea内置生成器或Angular CLI)运行它时,它可以正常工作并且可以发送请求并接收响应。 But when I run it from that server (same place that WebSrv server located) and run, it shows the first html page correctly but when I click the button for sending request, nothing happen (nor exception or any log) and server isn't receiving any request!! 但是,当我从该服务器(WebSrv服务器所在的位置)运行它并运行时,它正确显示了第一个html页面,但是当我单击发送请求的按钮时,没有任何反应(也没有异常或任何日志),并且服务器没有收到任何请求!

I've searched for my issue but there was nothing helpful.I Would be grateful if anyone could help me to find the solution. 我已经搜索了我的问题,但没有任何帮助。如果有人可以帮助我找到解决方案,我将不胜感激。 I was wondering if anyone knows what's the problem. 我想知道是否有人知道问题所在。

Here is my Client side code (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'] );
      }
    });

  }
}

and this is my server side piece of code (Spring Boot): 这是我的服务器端代码(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);
    }
}

Thanks in advance. 提前致谢。

The problem is the port number 4200 which was filtered by Network Department of company. 问题是由公司网络部门过滤的端口号4200 After changing the port to 8080 it works well. 将端口更改为8080它可以很好地工作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 在两个不同的端口中运行相同的JBoss服务器 - Run the same JBoss server in two different ports 在同一tomcat的不同端口上部署Angular 5和Spring Boot应用程序 - Deploy Angular 5 and spring boot applications on same tomcat at different ports 无法将Spring Boot Admin Server 2和Spring Boot Admin Client 2与Spring Boot 2集成在单个应用程序中 - Can't integrate Spring Boot Admin Server 2 and Spring Boot Admin Client 2 with Spring Boot 2 in a single application 同一台服务器上的 Spring Boot + Angular2 - Spring Boot + Angular2 on same server 如何将两个单独的项目(客户端和服务器端)部署到同一Google App Engine项目? - How can i deploy two separate projects (client and server side) to the same Google App Engine project? 我可以同时在不同的端口上运行硒服务器吗? - can I run selenium server on different ports at the same time? Spring Boot Server + Java客户端 - Spring boot server + java Client Spring Boot 应用程序在两个不同的端口上提供两个服务 - Spring boot application with two services on two different ports 在两个不同的端口上运行Tomcat服务器 - Running Tomcat server on two different ports Spring 引导管理:客户端无法通过 https 向管理服务器注册 - Spring boot admin: Client can't register with admin server over https
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM