简体   繁体   中英

Calling REST APIs on multiple ports using Spring Boot

My project uses Spring Boot . I have two Apps. The REST APIs in my first app run on port 8080 and the REST APIs in the second app run on port 8084.

I have a lot of REST calls in the JavaScript pages of the two apps. The problem is that these calls automatically go to port 8080. How can I change the port in some rest calls in the methods in my javascript ?

My JavaScript function is :

function loadRest() {
    const request = new XMLHttpRequest();
    request.onreadystatechange = function () {
        if (this.readyState === 4) {
            let result = parseResponse(this.status, this.responseText);
            if (result != null) {
                Rest.rests = result;
                createTable();
            }
        }
    };
    request.open("GET", Rest.baseURL + "/byCompany/" + logginedCompanyId, true);
    request.send();
}   

The REST calls automatically call port 8080. How can I change this?

The port that you are using is in the Rest.baseURL part.

You will need to change the variable somehow in your code to use port 8084 instead of port 8080.

This might be a simple workaround for you in case you cannot find a way to change the port in the Rest.baseURL at your end.

request.open("GET", Rest.baseURL.replace(":8080",":8084") + "/byCompany/" + logginedCompanyId, true);

I use .replace() to change the string ":8080" (port 8080) to ":8084" (port 8084).

finally i solved the problem with writing @CrossOrigin above the method in the controller .. its work . thank you

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM