'Unable to persist Spring data mongodb using save on repository

I am using Spring Data Mongodb and Embeded mongoDB to persist the data.

Pom.xml

    <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>
    </dependency>
    <dependency>
        <groupId>de.flapdoodle.embed</groupId>
        <artifactId>de.flapdoodle.embed.mongo</artifactId>

    </dependency>

applicaiton.properties

  spring.data.mongodb.port=2019
  spring.data.mongodb.database=testdb
  spring.data.mongodb.host=localhost

Main class:

   @SpringBootApplication 
  @EnableAutoConfiguration
  @EnableMongoRepositories(basePackages = { "com.koka.mongotest.repo"})
  public class MongotestApplication {

    public static void main(String[] args) {
    SpringApplication.run(MongotestApplication.class, args);
   }

}

Enity or Domain class :

  @Document
  public class Person 
   {
       @Indexed
       private String personId;
        private String name; 
        //getter and setters
    }

Repo:

    @Repository
   public interface PersonRepo extends MongoRepository { //No Custom emthod}

Service layer :

   @Service
   public class ServiceImpl {
         @Autowired
         PresonRepo repo;
        public void saveJon(Person p )
       {
         repo.save(p);
      }
   }

but Int DB its getting saved as

   {"_id":{"$oid":"60e18f9d7eb50d70b56b543f"},"_class":"com.koka.mongotest.entity.Person"}

But that person Object hold info which is not being saved. Please advise on this,



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source