Localhost vs 127.0.0.1 in Spring Framework

老子叫甜甜 提交于 2021-02-11 05:44:46

问题


I am attempting to connect a spring rest service to a client which is an android device.

I have followed this tutorial on creating my service: https://spring.io/guides/gs/rest-service/ Which by default runs the Spring Service on port 8080.

My issue is that on localhost:8080/test I get a correct output from my service but on 127.0.0.1:8080/test I get 404 Not found.

Is there any reason for this? I specifically need to connect to 127.0.0.1 for debugging purposes with an android emulator.

Thank you.


回答1:


This could be because Java 7+ is using IPv6 as the default instead of IPv4. Thus localhost resolves into :::1 instead of 127.0.0.1.

Try the following:

Just pass -Djava.net.preferIPv4Stack=true to java VM options.

If this works you can make it permanent by setting _JAVA_OPTIONS on Linux:

export _JAVA_OPTIONS="-Djava.net.preferIPv4Stack=true"

You can place the above line inside /etc/profile or /etc/profile.d/java.sh (by creating the file java.sh)



来源:https://stackoverflow.com/questions/35470838/localhost-vs-127-0-0-1-in-spring-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!