spring boot添加mongo-java驱动包之后报错的原因

由于在spring boot项目中需要连接公司服务器的mongo数据库,所以在pom文件中引入mongo-java的驱动包


    org.mongodb
    mongo-java-driver
    3.4.2


但是发现在启动项目的过程中,无论是否用到了这个包,都会包如下的exception:

com.mongodb.MongoSocketOpenException: Exception opening socket
at com.mongodb.connection.SocketStream.open(SocketStream.java:63)
at com.mongodb.connection.InternalStreamConnection.open(InternalStreamConnection.java:115)
at com.mongodb.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:113)
at java.lang.Thread.run(Thread.java:745)
Caused by: java.net.ConnectException: Connection refused: connect
at java.net.DualStackPlainSocketImpl.waitForConnect(Native Method)
at java.net.DualStackPlainSocketImpl.socketConnect(DualStackPlainSocketImpl.java:85)
at java.net.AbstractPlainSocketImpl.doConnect(AbstractPlainSocketImpl.java:350)
at java.net.AbstractPlainSocketImpl.connectToAddress(AbstractPlainSocketImpl.java:206)
at java.net.AbstractPlainSocketImpl.connect(AbstractPlainSocketImpl.java:188)
at java.net.PlainSocketImpl.connect(PlainSocketImpl.java:172)
at java.net.SocksSocketImpl.connect(SocksSocketImpl.java:392)
at java.net.Socket.connect(Socket.java:589)
at com.mongodb.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:57)
at com.mongodb.connection.SocketStream.open(SocketStream.java:58)
... 3 common frames omitted





后来经过上网查资料发现是由于spring boot默认加载了对于mongo的支持,在项目启动的过程中会默认的连接本地的mongo,而我本地并没有mongo数据库,所以导致报错。

解决方案:移除spring  boot默认的对于mongo的支持,在启动类的注解之上加上注解将mongo支持移除即可。


@EnableAutoConfiguration(exclude={MongoAutoConfiguration.class})

你可能感兴趣的:(exception记录)