Update
I've put two project on one server which are client and server RestAPI applications. I set 4200 as a port for the Client (which is written by Angular 6) and port 8990 for Server side WebSrv. I run Spring one with the command java -jar artifact.jar
, it works fine and response any requests. 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. 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!!
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):
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.