解決Spring Boot 在localhost域奇怪的404問(wèn)題(Mac book pro)
在mac系統(tǒng)中,明明url是對(duì)的,瀏覽器也可以打開(kāi),一個(gè)簡(jiǎn)單的代碼調(diào)用就是404,你有沒(méi)有遇到過(guò)?
情景再現(xiàn)
普通的一個(gè)controller,返回一個(gè)常量。
@GetMapping("/project_metadata/spring-boot") public String getMetadata(){ return "{\"data\":1234}";//這個(gè)不重要 }
調(diào)用接口的方式:
content = new JSONObject(restTemplate.getForObject(url, String.class));
大部分情況下,返回如下錯(cuò)誤,偶爾成功。
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]
排查
瀏覽器訪問(wèn)是正常的。
把localhost 改為一個(gè)私網(wǎng)IP,頁(yè)面空白,不報(bào)錯(cuò)。
到 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,恢復(fù)清明了。
坑在哪里?
有兩個(gè)進(jìn)程都在監(jiān)聽(tīng)8080,但ip錯(cuò)亂。
Mac osx 一手造成了坑。ubuntu 測(cè)試無(wú)坑,啟動(dòng)http-server的情況下,tomcat根本起不來(lái):
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
小結(jié):
完整的坑是這樣的,我用node起了一個(gè)127.0.0.1:8080 調(diào)js,完了沒(méi)關(guān)。
現(xiàn)在用springboot起8080,竟然成功,但這個(gè)坑就這么挖好了。
有兩個(gè)進(jìn)程都使用的8080,spring boot 是localhost:8080 ,他會(huì)精神錯(cuò)亂。因?yàn)閘ocalhost也是127.0.0.1。
奇了怪的是,既然錯(cuò)亂,啟動(dòng)的時(shí)候居然不報(bào)端口占用。
那么我們現(xiàn)在要明確,localhost指向127.0.0.1,但二者還是不一樣,localhost可以看做一個(gè)域名。
為了避免入坑,如果可能盡量不使用localhost,直接使用IP。
Tomcat 啟動(dòng)同樣的問(wèn)題。
瀏覽器一切正常,restTemplate錯(cuò)亂。
總結(jié)
以上所述是小編給大家介紹的解決Spring Boot 在localhost域奇怪的404問(wèn)題(Mac book pro),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
相關(guān)文章
maven倉(cāng)庫(kù)repositories和mirrors的配置及區(qū)別詳解
這篇文章主要介紹了maven倉(cāng)庫(kù)repositories和mirrors的配置及區(qū)別詳解,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2020-07-07詳解SpringBoot中異步請(qǐng)求的實(shí)現(xiàn)與并行執(zhí)行
這篇文章主要為大家詳細(xì)介紹了在SpringBoot中如何是實(shí)現(xiàn)異步請(qǐng)求、并行執(zhí)行,文中的示例代碼講解詳細(xì),感興趣的小伙伴可以跟隨小編一起學(xué)習(xí)一下2024-02-02java構(gòu)建Stream流的多種方式總結(jié)
Java?8引入了Stream流作為一項(xiàng)新的特性,它是用來(lái)處理集合數(shù)據(jù)的一種函數(shù)式編程方式,本文為大家整理了多種java構(gòu)建Stream流的方式,希望對(duì)大家有所幫助2023-11-11由@NotNull注解引出的關(guān)于Java空指針的控制
這是一些很容易學(xué)會(huì)的簡(jiǎn)單技術(shù),但是對(duì)于代碼質(zhì)量和健壯性來(lái)說(shuō)確實(shí)很重要。以我的經(jīng)驗(yàn),僅是第一個(gè)小技巧就已經(jīng)對(duì)改進(jìn)代碼質(zhì)量具有很大的作用了2016-09-09mybatis攔截器無(wú)法注入spring bean的問(wèn)題解決
本文主要介紹了mybatis攔截器無(wú)法注入spring bean的問(wèn)題解決,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2022-02-02java幾種排序算法的實(shí)現(xiàn)及簡(jiǎn)單分析
這篇文章主要介紹了java幾種排序算法的實(shí)現(xiàn)及簡(jiǎn)單分析,實(shí)例分析了插入排序、希爾排序、選擇排序等常用排序算法,并分析了各個(gè)算法的優(yōu)劣,需要的朋友可以參考下2015-05-05SpringBoot實(shí)現(xiàn)devtools實(shí)現(xiàn)熱部署過(guò)程解析
這篇文章主要介紹了SpringBoot實(shí)現(xiàn)devtools實(shí)現(xiàn)熱部署過(guò)程解析,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-03-03Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn)
日常工作或者學(xué)習(xí)中,可能會(huì)遇到批量插入數(shù)據(jù)的需求,一般情況下數(shù)據(jù)量少的時(shí)候,我們會(huì)直接調(diào)用批量接口插入數(shù)據(jù)即可,當(dāng)數(shù)據(jù)量特別大時(shí),我們就會(huì)用到分批插入數(shù)據(jù),所以本文給大家介紹了Java批量插入數(shù)據(jù)的代碼實(shí)現(xiàn),需要的朋友可以參考下2024-01-01java中fastjson生成和解析json數(shù)據(jù)(序列化和反序列化數(shù)據(jù))
本篇文章主要介紹了java中fastjson生成和解析json數(shù)據(jù)(序列化和反序列化數(shù)據(jù)),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下。2017-02-02