在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),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對創新互聯網站的支持!
網站題目:解決SpringBoot在localhost域奇怪的404問題(Macbookpro)
文章分享:http://m.2m8n56k.cn/article48/jdcdhp.html
成都網站建設公司_創新互聯,為您提供動態網站、微信小程序、網站維護、搜索引擎優化、移動網站建設、自適應網站
聲明:本網站發布的內容(圖片、視頻和文字)以用戶投稿、用戶轉載內容為主,如果涉及侵權請盡快告知,我們將會在第一時間刪除。文章觀點不代表本網站立場,如需處理請聯系客服。電話:028-86922220;郵箱:631063699@qq.com。內容未經允許不得轉載,或轉載時需注明來源: 創新互聯