在類SpringbootdemoApplication上右鍵Run as選擇Spring Boot App後Console輸出報錯日誌如下: com.mongodb.MongoSocketOpenException: Exception opening socketat com.mongodb.co ...
在類SpringbootdemoApplication上右鍵Run as選擇Spring Boot App後Console輸出報錯日誌如下:
com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113) ~[mongodb-driver-core-3.4.2.jar:na]
at java.lang.Thread.run(Unknown Source) [na:1.8.0_91]
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method) ~[na:1.8.0_91]
at java.net.DualStackPlainSocketImpl.socketConnect(Unknown Source) ~[na:1.8.0_91]
at java.net.AbstractPlainSocketImpl.doConnect(Unknown Source) ~[na:1.8.0_91]
at java.net.AbstractPlainSocketImpl.connectToAddress(Unknown Source) ~[na:1.8.0_91]
at java.net.AbstractPlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_91]
at java.net.PlainSocketImpl.connect(Unknown Source) ~[na:1.8.0_91]
at java.net.SocksSocketImpl.connect(Unknown Source) ~[na:1.8.0_91]
at java.net.Socket.connect(Unknown Source) ~[na:1.8.0_91]
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57) ~[mongodb-driver-core-3.4.2.jar:na]
at com.mongodb.connection.SocketStream.open(SocketStream.java:58) ~[mongodb-driver-core-3.4.2.jar:na]
... 3 common frames omitted
上網百度,找到一篇文章說原因是springboot自動配置了支持mongodb。在啟動springboot時會自動實例化一個mongo實例,需要禁用自動配置 ,
增加@SpringBootApplication(exclude = MongoAutoConfiguration.class)這個註解即可,原文鏈接為http://blog.csdn.net/xuyw10000/article/details/72918360。
我增加在類上增加註解後,這個錯誤解決了,但是輸出了新的錯誤日誌,如下:
java.lang.ClassCastException: org.springframework.boot.context.event.ApplicationFailedEvent cannot be cast to org.springframework.boot.web.context.WebServerInitializedEvent
at org.springframework.context.event.SimpleApplicationEventMulticaster.invokeListener(SimpleApplicationEventMulticaster.java:159) [spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:139) [spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.context.event.SimpleApplicationEventMulticaster.multicastEvent(SimpleApplicationEventMulticaster.java:127) [spring-context-5.0.0.RC3.jar:5.0.0.RC3]
at org.springframework.boot.context.event.EventPublishingRunListener.finished(EventPublishingRunListener.java:114) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.boot.SpringApplicationRunListeners.callFinishedListener(SpringApplicationRunListeners.java:79) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.boot.SpringApplicationRunListeners.finished(SpringApplicationRunListeners.java:72) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.boot.SpringApplication.handleRunFailure(SpringApplication.java:803) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:338) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1245) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
at org.springframework.boot.SpringApplication.run(SpringApplication.java:1233) [spring-boot-2.0.0.M3.jar:2.0.0.M3]
at com.example.demo.SpringbootdemoApplication.main(SpringbootdemoApplication.java:22) [classes/:na],
複製第一行錯誤日誌,在谷歌上搜索答案,需要該項目的pom.xml文件註釋Mongo相關的啟動語句,如下:
<!-- <dependency>
<groupId>org.springframework.boot</groupId>
<artifactId>spring-boot-starter-data-mongodb</artifactId>
</dependency> -->
保存後重新啟動,不會再報錯。在github原文鏈接為:https://github.com/spring-projects/spring-boot/issues/10047。