2

I'm trying to make a simple progam with Spring Boot work with MongoDB in Spring Boot 2.3.9 From the startup logs I suspect that something is wrong, it seems like it's initialized twice.

This is my console output:

2021-03-08 01:54:43.515 INFO 26609 --- [ restartedMain] c.a.a.r.SpringbootRegistroApplication : Starting SpringbootRegistroApplication on santiagoVB with PID 26609 (/home/santiago/Documentos/workspace/springboot-registro/target/classes started by santiago in /home/santiago/Documentos/workspace/springboot-registro) 2021-03-08 01:54:43.521 INFO 26609 --- [ restartedMain] c.a.a.r.SpringbootRegistroApplication : No active profile set, falling back to default profiles: default 2021-03-08 01:54:43.680 INFO 26609 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : Devtools property defaults active! Set 'spring.devtools.add-properties' to 'false' to disable 2021-03-08 01:54:43.682 INFO 26609 --- [ restartedMain] .e.DevToolsPropertyDefaultsPostProcessor : For additional web related logging consider setting the 'logging.level.web' property to 'DEBUG' 2021-03-08 01:54:45.719 INFO 26609 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Bootstrapping Spring Data MongoDB repositories in DEFAULT mode. 2021-03-08 01:54:45.895 INFO 26609 --- [ restartedMain] .s.d.r.c.RepositoryConfigurationDelegate : Finished Spring Data repository scanning in 167ms. Found 1 MongoDB repository interfaces. 2021-03-08 01:54:46.683 INFO 26609 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat initialized with port(s): 8080 (http) 2021-03-08 01:54:46.699 INFO 26609 --- [ restartedMain] o.apache.catalina.core.StandardService : Starting service [Tomcat] 2021-03-08 01:54:46.701 INFO 26609 --- [ restartedMain] org.apache.catalina.core.StandardEngine : Starting Servlet engine: [Apache Tomcat/9.0.43] 2021-03-08 01:54:46.830 INFO 26609 --- [ restartedMain] o.a.c.c.C.[Tomcat].[localhost].[/] : Initializing Spring embedded WebApplicationContext 2021-03-08 01:54:46.830 INFO 26609 --- [ restartedMain] w.s.c.ServletWebServerApplicationContext : Root WebApplicationContext: initialization completed in 3147 ms 2021-03-08 01:54:47.152 INFO 26609 --- [ restartedMain] org.mongodb.driver.cluster : Cluster created with settings {hosts=[localhost:27017], mode=SINGLE, requiredClusterType=UNKNOWN, serverSelectionTimeout='30000 ms'} 2021-03-08 01:54:47.363 INFO 26609 --- [localhost:27017] org.mongodb.driver.connection : Opened connection [connectionId{localValue:1, serverValue:12}] to localhost:27017 2021-03-08 01:54:47.372 INFO 26609 --- [localhost:27017] org.mongodb.driver.cluster : Monitor thread successfully connected to server with description ServerDescription{address=localhost:27017, type=STANDALONE, state=CONNECTED, ok=true, minWireVersion=0, maxWireVersion=9, maxDocumentSize=16777216, logicalSessionTimeoutMinutes=30, roundTripTimeNanos=5581801} 2021-03-08 01:54:47.714 INFO 26609 --- [ restartedMain] o.s.b.d.a.OptionalLiveReloadServer : LiveReload server is running on port 35729 2021-03-08 01:54:48.326 INFO 26609 --- [ restartedMain] o.s.s.concurrent.ThreadPoolTaskExecutor : Initializing ExecutorService 'applicationTaskExecutor' 2021-03-08 01:54:48.685 INFO 26609 --- [ restartedMain] o.s.b.w.embedded.tomcat.TomcatWebServer : Tomcat started on port(s): 8080 (http) with context path '' 2021-03-08 01:54:48.701 INFO 26609 --- [ restartedMain] c.a.a.r.SpringbootRegistroApplication : Started SpringbootRegistroApplication in 6.21 seconds (JVM running for 9.239) 

My pom.xml:

<?xml version="1.0" encoding="UTF-8"?> <project xmlns="http://maven.apache.org/POM/4.0.0" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 https://maven.apache.org/xsd/maven-4.0.0.xsd"> <modelVersion>4.0.0</modelVersion> <parent> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-parent</artifactId> <version>2.3.9.RELEASE</version> <relativePath /> <!-- lookup parent from repository --> </parent> <groupId>com.appcity.app.registro</groupId> <artifactId>springboot-registro</artifactId> <version>0.0.1-SNAPSHOT</version> <name>springboot-registro</name> <description>Demo project for Spring Boot</description> <properties> <java.version>15</java.version> </properties> <dependencies> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-data-mongodb</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-web</artifactId> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-devtools</artifactId> <scope>runtime</scope> <optional>true</optional> </dependency> <dependency> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-starter-test</artifactId> <scope>test</scope> <exclusions> <exclusion> <groupId>org.junit.vintage</groupId> <artifactId>junit-vintage-engine</artifactId> </exclusion> </exclusions> </dependency> </dependencies> <build> <plugins> <plugin> <groupId>org.springframework.boot</groupId> <artifactId>spring-boot-maven-plugin</artifactId> </plugin> </plugins> </build> </project> 

My application.properties:

spring.data.mongodb.uri=mongodb://localhost:27017/App spring.data.mongodb.auto-index-creation=true #spring.data.mongodb.host=localhost #spring.data.mongodb.port=27017 #spring.data.mongodb.username=Udea #spring.data.mongodb.password=udeapp #spring.data.mongodb.database=App 

My Object:

package com.appcity.app.registro.models.entity; import org.springframework.data.annotation.Id; import org.springframework.data.mongodb.core.mapping.Document; @Document(collection = "UsuarioDb") public class UsuarioDb { @Id private String id; private String username; private String phone; private String email; private String password; public UsuarioDb() { super(); } public UsuarioDb(String id, String username, String phone, String email, String password) { super(); this.id = id; this.username = username; this.phone = phone; this.email = email; this.password = password; } public String getId() { return id; } public void setId(String id) { this.id = id; } public String getUsername() { return username; } public void setUsername(String username) { this.username = username; } public String getPhone() { return phone; } public void setPhone(String phone) { this.phone = phone; } public String getEmail() { return email; } public void setEmail(String email) { this.email = email; } public String getPassword() { return password; } public void setPassword(String password) { this.password = password; } @Override public String toString() { return "UsuarioDb [id=" + id + ", username=" + username + ", phone=" + phone + ", email=" + email + ", password=" + password + "]"; } } 

My Interface:

package com.appcity.app.registro.models.dao; import org.springframework.data.mongodb.repository.MongoRepository; import org.springframework.stereotype.Repository; import com.appcity.app.registro.models.entity.UsuarioDb; @Repository public interface RegistroDao extends MongoRepository<UsuarioDb, String>{ } 

My Controller:

package com.appcity.app.registro.controllers; import java.util.List; import java.util.Optional; import org.springframework.beans.factory.annotation.Autowired; import org.springframework.web.bind.annotation.DeleteMapping; import org.springframework.web.bind.annotation.GetMapping; import org.springframework.web.bind.annotation.PathVariable; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestBody; import org.springframework.web.bind.annotation.RestController; import com.appcity.app.registro.models.dao.RegistroDao; import com.appcity.app.registro.models.entity.UsuarioDb; //@CrossOrigin @RestController public class RegistroController { @Autowired private RegistroDao repository; @PostMapping("/registro/crear") public String saveUsuarioDb(@RequestBody UsuarioDb usuarioDb) { repository.save(usuarioDb); return "Added usuarioDb with id : " + usuarioDb.getId(); } @GetMapping("/registro/listar") public List<UsuarioDb> getUsers(){ return repository.findAll(); } @GetMapping("/registro/listar/{id}") public Optional<UsuarioDb> getUser(@PathVariable String id){ return repository.findById(id); } @DeleteMapping("/registro/eliminar/{id}") public String deleteUser(@PathVariable String id) { repository.deleteById(id); return "usuarioDb deleted with id : "+id; } } 

My MongoDb Status in Ubuntu:

● mongod.service - MongoDB Database Server Loaded: loaded (/lib/systemd/system/mongod.service; enabled; vendor preset: enabled) Active: active (running) since Mon 2021-03-08 00:30:58 -05; 1h 46min ago Docs: https://docs.mongodb.org/manual Main PID: 21445 (mongod) Memory: 160.1M CGroup: /system.slice/mongod.service └─21445 /usr/bin/mongod --config /etc/mongod.conf mar 08 00:30:58 santiagoVB systemd[1]: Started MongoDB Database Server. 

My database in MongoDb:

show dbs App 0.000GB admin 0.000GB config 0.000GB local 0.000GB 

What am I doing wrong? What configuration do I have wrong?

8
  • 2
    Why do you thoink there is no conection? I can not see any error in your log Commented Mar 8, 2021 at 7:33
  • Because the servlet dispatcherServlet mapped to [/] and the mapping filters is not created, I do the test with postman, the program runs but does not create any document in the mongo collection Commented Mar 8, 2021 at 7:40
  • 1
    What http return code do you get? Commented Mar 8, 2021 at 7:42
  • Add the ResponseStatus in RegistroController and Postman Return 201 Created Commented Mar 8, 2021 at 8:55
  • Are you able use the repository methods from Spring Boot CommandLineRunner and see any results? Commented Mar 8, 2021 at 9:16

1 Answer 1

1

I ran into a similar kind of issue and I installed a different JDK patch. I had JDK 11.0.2 and I changed it to JDK 11.0.10 and It worked for me.

Also, you need to have the @CrossOrigin(origins = "*") annotation. * (asterisk) means you are allowing requests from any origin.

Sign up to request clarification or add additional context in comments.

2 Comments

could you specify, which one?
It would be helpful to edit the answer with all relevant information

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.