Fix Spring Boot strange 404 problem in localhost domain of Mac book pro
- 2020-10-23 20:06:46
- OfStack
In mac system, although url is correct, the browser can also be opened, a simple code call is 404, have you ever encountered?
Scene reappearance
Normal 1 controller, returns 1 constant.
@GetMapping("/project_metadata/spring-boot")
public String getMetadata(){
return "{\"data\":1234}";// It doesn't matter
}
How to call the interface:
content = new JSONObject(restTemplate.getForObject(url, String.class));
In most cases, the following error is returned, with occasional success.
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]
screening
Browser access is normal.
localhost to a private network IP, page blank, no error.
Go to bash to see:
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
What's going on?
Check localhost again:
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
View port:
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)
What the hell?
Kill node and clear is restored.
Where is the pit?
There are two processes listening on 8080, but ip is not working.
Mac osx 1 hand creates a pit. ubuntu test no pit, es51EN-server started, tomcat can not get up at all:
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
Summary:
The complete pit is like this, I used node to start a 127.0.0.1:8080 js, finished.
Now using springboot 8080, it works, but the hole is there.
There are two processes using 8080, spring boot is localhost:8080, he will be deranged. Because localhost is also 127.0.0.1.
Strangely enough, since the confusion, the startup did not report the mouth occupancy.
So now we want to make it clear that localhost points to 127.0.0.1, but the two is still not the same, and localhost can be regarded as a domain name.
To avoid pit entry, avoid using localhost if possible and use IP instead.
Tomcat starts the same problem.
Browser 1 cut normal, restTemplate confused.
conclusion