如何从远程机器访问REST API?

huangapple go评论64阅读模式
英文:

How to access REST API from a remote machine?

问题

我正在为移动应用编写Spring服务(REST)。我正在使用本地主机编写测试服务,并在虚拟机(Windows)上运行,但我只能在同一台计算机上看到http://localhost:8080/rest。我应该怎么做才能从另一台机器上使用我的REST API?IP

应用程序属性:

spring.data.mongodb.host=localhost
spring.data.mongodb.port=
spring.data.mongodb.database=mongoTest
英文:

I am writting spring service(rest) for mobile app.I am writing test service with localhost and running on virtual machine(windows) but i can see http://localhost:8080/restonly on the same computer.What should i do to use my rest api from another machine?ip

aplication properties

spring.data.mongodb.host=localhost
spring.data.mongodb.port=
spring.data.mongodb.database=mongoTest

答案1

得分: 1

你在谈论两件不同的事情。

你的属性文件是关于连接到你的 MongoDB 实例的。

localhost 只是一个简化方式,表示应用程序可以在你的本地机器上找到。

在此属性文件中使用 localhost 意味着应用程序服务器和数据库服务器位于同一台物理机器上。

如果你想从另一台机器访问你的 API,可以使用服务器的 IP 地址。

在 Windows 上,你可以通过在命令行中输入 ipconfig 来获取这个地址。

在 Linux 上,可以使用 ifconfig。

之后,你可以尝试使用机器名而不是 IP 地址。

英文:

You are talking about two different things.

Your property file is about connection to your mongodb instance.

localhost is only a shortcut "saying" that the application can be found on your local machine.

having localhost in this property file means that the application server and the database server are on the same physical machine.

If you want to access your API from another machine, you can use the IP address of the server.

On Windows, you can know this address by typing ipconfig in a command line.

On Linux, it would be ifconfig.

Later, you can try to work with machine name instead of the IP address.

huangapple
  • 本文由 发表于 2020年8月16日 22:23:28
  • 转载请务必保留本文链接:https://go.coder-hub.com/63438017.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定