title: com.mongodb.MongoSocketOpenException: Exception opening socket with spring boot and mongodb cloud
body: <p>Please suggest any solution for the below exception which is occuring while running my rest api while executing springboot application with mongodb cloud db. I have created collection in mongodb as well and configured below properties.</p>
  <pre><code>com.mongodb.MongoSocketOpenException: Exception opening socket
      at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:70) ~[mongodb-driver-core-4.1.1.jar:na]
      at com.mongodb.internal.connection.InternalStreamConnection.open(InternalStreamConnection.java:143) ~[mongodb-driver-core-4.1.1.jar:na]
      at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.lookupServerDescription(DefaultServerMonitor.java:188) ~[mongodb-driver-core-4.1.1.jar:na]
      at com.mongodb.internal.connection.DefaultServerMonitor$ServerMonitorRunnable.run(DefaultServerMonitor.java:144) ~[mongodb-driver-core-4.1.1.jar:na]
      at java.base/java.lang.Thread.run(Thread.java:832) ~[na:na]
  Caused by: java.net.ConnectException: Connection refused: no further information
      at java.base/sun.nio.ch.Net.pollConnect(Native Method) ~[na:na]
      at java.base/sun.nio.ch.Net.pollConnectNow(Net.java:589) ~[na:na]
      at java.base/sun.nio.ch.NioSocketImpl.timedFinishConnect(NioSocketImpl.java:549) ~[na:na]
      at java.base/sun.nio.ch.NioSocketImpl.connect(NioSocketImpl.java:597) ~[na:na]
      at java.base/java.net.SocksSocketImpl.connect(SocksSocketImpl.java:333) ~[na:na]
      at java.base/java.net.Socket.connect(Socket.java:648) ~[na:na]
      at com.mongodb.internal.connection.SocketStreamHelper.initialize(SocketStreamHelper.java:78) ~[mongodb-driver-core-4.1.1.jar:na]
      at com.mongodb.internal.connection.SocketStream.initializeSocket(SocketStream.java:79) ~[mongodb-driver-core-4.1.1.jar:na]
      at com.mongodb.internal.connection.SocketStream.open(SocketStream.java:65) ~[mongodb-driver-core-4.1.1.jar:na]
      ... 4 common frames omitted
  </code></pre>
  <pre><code>Model class
  ------------
  package com.praveen.entity;
  
  import org.springframework.data.annotation.Id;
  import org.springframework.data.mongodb.core.mapping.Document;
  
  import lombok.Data;
  
  @Document
  public class Products {
  
      @Id
      private String id;
      private Integer p_id;
      private String p_name;
      private Integer p_cost;
      private Integer countInStock;
      private Integer numReviews;
      private String image;
      
      public String getId() {
          return id;
      }
      public void setId(String id) {
          this.id = id;
      }
      public Integer getP_id() {
          return p_id;
      }
      public void setP_id(Integer p_id) {
          this.p_id = p_id;
      }
      public String getP_name() {
          return p_name;
      }
      public void setP_name(String p_name) {
          this.p_name = p_name;
      }
      public Integer getP_cost() {
          return p_cost;
      }
      public void setP_cost(Integer p_cost) {
          this.p_cost = p_cost;
      }
      public Integer getCountInStock() {
          return countInStock;
      }
      public void setCountInStock(Integer countInStock) {
          this.countInStock = countInStock;
      }
      public Integer getNumReviews() {
          return numReviews;
      }
      public void setNumReviews(Integer numReviews) {
          this.numReviews = numReviews;
      }
      public String getImage() {
          return image;
      }
      public void setImage(String image) {
          this.image = image;
      }
      @Override
      public String toString() {
          return &quot;Products [id=&quot; + id + &quot;, p_id=&quot; + p_id + &quot;, p_name=&quot; + p_name + &quot;, p_cost=&quot; + p_cost + &quot;, countInStock=&quot;
                  + countInStock + &quot;, numReviews=&quot; + numReviews + &quot;, image=&quot; + image + &quot;]&quot;;
      }
      
      
  }
  
  </code></pre>
  <pre><code>RestController class
  ---------------------
  
  package com.praveen.rest;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.http.HttpStatus;
  import org.springframework.http.ResponseEntity;
  import org.springframework.web.bind.annotation.RestController;
  
  import com.praveen.entity.Products;
  import com.praveen.service.ProductsService;
  
  @RestController
  public class ProductsRestController {
  
      @Autowired
      private ProductsService service;
      
      @GetMapping(&quot;/products&quot;)
      public ResponseEntity&lt;List&lt;Products&gt;&gt; getProducts(){
          List&lt;Products&gt; allProducts = service.getAllProducts();
          if(allProducts != null) {
              return new ResponseEntity&lt;List&lt;Products&gt;&gt;(allProducts, HttpStatus.OK);
          }else {
              return new ResponseEntity&lt;List&lt;Products&gt;&gt;(HttpStatus.BAD_REQUEST);
          }
      }
  }
  
  </code></pre>
  <pre><code>Service class
  --------------
  package com.praveen.service;
  
  import java.util.List;
  
  import org.springframework.beans.factory.annotation.Autowired;
  import org.springframework.stereotype.Service;
  
  import com.praveen.entity.Products;
  import com.praveen.repository.ProductsRepository;
  
  @Service
  public class ProductsServiceImpl implements ProductsService {
  
      @Autowired
      private ProductsRepository prodRepo;
      
      @Override
      public List&lt;Products&gt; getAllProducts() {
          List&lt;Products&gt; productsList = prodRepo.findAll();
          if(productsList != null) {
              System.out.println(productsList);
              return productsList;
          }
          return null;
      }
  
  }
  
  </code></pre>
  <pre><code>Repository 
  -----------
  package com.praveen.repository;
  
  import java.io.Serializable;
  
  import org.springframework.data.mongodb.repository.MongoRepository;
  import org.springframework.stereotype.Repository;
  
  import com.praveen.entity.Products;
  
  @Repository
  public interface ProductsRepository extends MongoRepository&lt;Products, Serializable&gt; {
  
  }
  
  </code></pre>
  <pre><code>application.properties
  -----------------------
  spring.data.mongodb.url = mongodb+srv://praveen:praveen@cluster0.qrvkm.mongodb.net/miniproject?retryWrites=true&amp;w=majority
  spring.data.mongodb.database = products
  spring.data.mongodb.port = 27017
  spring.data.mongodb.repositories.enabled=true
  </code></pre>
  <pre><code>pom.xml
  -------
  &lt;?xml version=&quot;1.0&quot; encoding=&quot;UTF-8&quot;?&gt;
  &lt;project xmlns=&quot;http://maven.apache.org/POM/4.0.0&quot; xmlns:xsi=&quot;http://www.w3.org/2001/XMLSchema-instance&quot;
      xsi:schemaLocation=&quot;http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd&quot;&gt;
      &lt;modelVersion&gt;4.0.0&lt;/modelVersion&gt;
      &lt;parent&gt;
          &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
          &lt;artifactId&gt;spring-boot-starter-parent&lt;/artifactId&gt;
          &lt;version&gt;2.4.2&lt;/version&gt;
          &lt;relativePath/&gt; &lt;!-- lookup parent from repository --&gt;
      &lt;/parent&gt;
      &lt;groupId&gt;com.praveen&lt;/groupId&gt;
      &lt;artifactId&gt;NgRx-Products&lt;/artifactId&gt;
      &lt;version&gt;0.0.1-SNAPSHOT&lt;/version&gt;
      &lt;packaging&gt;war&lt;/packaging&gt;
      &lt;name&gt;05-MiniProject-NgRx-Products&lt;/name&gt;
      &lt;description&gt;NgRx package with Products data using springboot&lt;/description&gt;
      &lt;properties&gt;
          &lt;java.version&gt;15&lt;/java.version&gt;
      &lt;/properties&gt;
      &lt;dependencies&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-data-mongodb&lt;/artifactId&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-web&lt;/artifactId&gt;
          &lt;/dependency&gt;
  
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-devtools&lt;/artifactId&gt;
              &lt;scope&gt;runtime&lt;/scope&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
              &lt;optional&gt;true&lt;/optional&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-tomcat&lt;/artifactId&gt;
              &lt;scope&gt;provided&lt;/scope&gt;
          &lt;/dependency&gt;
          &lt;dependency&gt;
              &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
              &lt;artifactId&gt;spring-boot-starter-test&lt;/artifactId&gt;
              &lt;scope&gt;test&lt;/scope&gt;
          &lt;/dependency&gt;
      &lt;/dependencies&gt;
  
      &lt;build&gt;
          &lt;plugins&gt;
              &lt;plugin&gt;
                  &lt;groupId&gt;org.springframework.boot&lt;/groupId&gt;
                  &lt;artifactId&gt;spring-boot-maven-plugin&lt;/artifactId&gt;
                  &lt;configuration&gt;
                      &lt;excludes&gt;
                          &lt;exclude&gt;
                              &lt;groupId&gt;org.projectlombok&lt;/groupId&gt;
                              &lt;artifactId&gt;lombok&lt;/artifactId&gt;
                          &lt;/exclude&gt;
                      &lt;/excludes&gt;
                  &lt;/configuration&gt;
              &lt;/plugin&gt;
          &lt;/plugins&gt;
      &lt;/build&gt;
  
  &lt;/project&gt;
  
  </code></pre>
  <p>Please suggest any solution for the above exception, I have given all my files in the above.</p>