当前位置: 移动技术网 > IT编程>开发语言>Java > 解决Spring Boot 在localhost域奇怪的404问题(Mac book pro)

解决Spring Boot 在localhost域奇怪的404问题(Mac book pro)

2019年07月19日  | 移动技术网IT编程  | 我要评论

在mac系统中,明明url是对的,浏览器也可以打开,一个简单的代码调用就是404,你有没有遇到过?

情景再现

普通的一个controller,返回一个常量。

@getmapping("/project_metadata/spring-boot")
public string getmetadata(){
 return "{\"data\":1234}";//这个不重要
}

调用接口的方式:

content = new jsonobject(resttemplate.getforobject(url, string.class));

大部分情况下,返回如下错误,偶尔成功。

2017-08-31 14:35:38.867 info 3450 --- [nio-8080-exec-1] .i.w.s.defaultinitializrmetadataprovider : fetching boot metadata from http://localhost:8080/project_metadata/spring-boot
2017-08-31 14:35:38.872 warn 3450 --- [nio-8080-exec-1] .i.w.s.defaultinitializrmetadataprovider : failed to fetch spring boot metadata
org.springframework.web.client.httpclienterrorexception: 404 not found
 at org.springframework.web.client.defaultresponseerrorhandler.handleerror(defaultresponseerrorhandler.java:63) ~[spring-web-4.3.10.release.jar:4.3.10.release]
 at org.springframework.web.client.resttemplate.handleresponse(resttemplate.java:700) ~[spring-web-4.3.10.release.jar:4.3.10.release]
 at org.springframework.web.client.resttemplate.doexecute(resttemplate.java:653) ~[spring-web-4.3.10.release.jar:4.3.10.release]
 at org.springframework.web.client.resttemplate.execute(resttemplate.java:613) ~[spring-web-4.3.10.release.jar:4.3.10.release]
 at org.springframework.web.client.resttemplate.getforobject(resttemplate.java:287) ~[spring-web-4.3.10.release.jar:4.3.10.release]

排查

浏览器访问是正常的。

把localhost 改为一个私网ip,页面空白,不报错。

到 bash中查看:

curl -i http://10.2.10.203:8080/project_metadata/spring-boot
http/1.1 404 not found
server: ecstatic-1.4.1
date: thu, 31 aug 2017 07:06:39 gmt
connection: keep-alive

什么情况?

再次检查localhost:

curl -i http://localhost:8080/project_metadata/spring-boot
http/1.1 200
content-type: application/json;charset=utf-8
content-length: 2683
date: thu, 31 aug 2017 07:07:28 gmt

查看端口:

lsof -i:8080
command pid   user  fd  type       device size/off node name
node  1045 pollyduan  13u ipv4 0x992085ef857b1d07   0t0 tcp *:http-alt (listen)
java  3995 pollyduan  65u ipv6 0x992085ef905d994f   0t0 tcp *:http-alt (listen)

什么鬼?

杀掉node,恢复清明了。

坑在哪里?

有两个进程都在监听8080,但ip错乱。

mac osx 一手造成了坑。ubuntu 测试无坑,启动http-server的情况下,tomcat根本起不来:

caused by: java.net.bindexception: address already in use
 at sun.nio.ch.net.bind0(native method)
 at sun.nio.ch.net.bind(net.java:433)
 at sun.nio.ch.net.bind(net.java:425)
 at sun.nio.ch.serversocketchannelimpl.bind(serversocketchannelimpl.java:223)
 at sun.nio.ch.serversocketadaptor.bind(serversocketadaptor.java:74)
 at org.apache.tomcat.util.net.nioendpoint.bind(nioendpoint.java:340)
 at org.apache.tomcat.util.net.abstractendpoint.init(abstractendpoint.java:742)
 at org.apache.coyote.abstractprotocol.init(abstractprotocol.java:458)
 at org.apache.coyote.http11.abstracthttp11jsseprotocol.init(abstracthttp11jsseprotocol.java:120)
 at org.apache.catalina.connector.connector.initinternal(connector.java:960)
 ... 13 more

小结:

完整的坑是这样的,我用node起了一个127.0.0.1:8080 调js,完了没关。

现在用springboot起8080,竟然成功,但这个坑就这么挖好了。

有两个进程都使用的8080,spring boot 是localhost:8080 ,他会精神错乱。因为localhost也是127.0.0.1。

奇了怪的是,既然错乱,启动的时候居然不报端口占用。

那么我们现在要明确,localhost指向127.0.0.1,但二者还是不一样,localhost可以看做一个域名。

为了避免入坑,如果可能尽量不使用localhost,直接使用ip。

tomcat 启动同样的问题。

浏览器一切正常,resttemplate错乱。

总结

以上所述是小编给大家介绍的解决spring boot 在localhost域奇怪的404问题(mac book pro),希望对大家有所帮助

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网