'No error in API but in MongoDB my data is not getting initialized

I am newbie coder and I am trying to create a Spring boot API using MongoDB. This is the schema template of the database.

{
"id":"string",
"user_name":"string"
"product":[
  {
    "product_id":"string",
    "prod_name":"string",
    "price":"double",
    "quantity":"number",
    
  }],
  "total":"double"
}

This is my entity class User.java

@Getter
@Setter
@NoArgsConstructor
@Document(collection="user")
public class User {
    
    @Id
    private String id;
    
    
    @Indexed(unique=true)
    private String user_name;
    
    private Product product;
    
    
    private double total;
    

    
}

This is another entity class Product.java

@Getter
@Setter
@Document(collection="product")
public class Product {
    
    @Id
    private String prod_id;
    
    @Indexed(unique=true)
    private String prod_name;
    
    private double price;
    private int quantity;
        

}

This is my UserRepository.java interface

@Repository
public interface UserRepository extends MongoRepository<User,String> {
}

This is my UserService.java interface

public interface UserService {
    void saveUser(User user);

}

This is my UserServiceImpl.java class

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    UserRepository userRepo;
    
    @Override
    public void saveUser(User user) {
        
        userRepo.save(user);        
    }

}

This is my UserController.java class


@RestController
@RequestMapping("/usercart")
public class UserController {
    
    @Autowired
    private UserService userService;
    
    @Autowired
    private UserRepository userRepository;
    
    
    @GetMapping("/")
    public List<User> getCartInfo()
    {
        return userRepository.findAll();
    }
    
    @PostMapping("/order")
    public ResponseEntity<?> add(@RequestBody User user)
    {
        userService.saveUser(user);
        return  new ResponseEntity<>(HttpStatus.OK);
        
    }
}

This is 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.6.2</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.demo</groupId>
    <artifactId>cartDemo</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>cartDemo</name>
    <description>Demo project for Spring Boot</description>
    <properties>
        <java.version>11</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-data-mongodb-reactive</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-webflux</artifactId>
        </dependency>

        <dependency>
            <groupId>org.projectlombok</groupId>
            <artifactId>lombok</artifactId>
            <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>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>io.projectreactor</groupId>
            <artifactId>reactor-test</artifactId>
            <scope>test</scope>
        </dependency>
    </dependencies>

    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
                <configuration>
                    <excludes>
                        <exclude>
                            <groupId>org.projectlombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

This is my application.properties file

spring.data.mongodb.host=localhost
spring.data.mongodb.port=27017
spring.data.mongodb.database=UserCart

When I try to use post method like

 Body -
{ "id":"1222", 
"user_name":"shivam", 
"product":[{
 "prod_id":"w121", "prod_name":"wheel",
 "price":10,
 "quantity":1 }], 
"total":10 }.
 My MongoDB object looks like - _id:ObjectId("e6789") 
total :0, _class:"com.trial.spring.api.user.User" 


Solution 1:[1]

I don't see your property file here, but most likely you are missing below properties in your application.properties file:

spring.data.mongodb.uri=mongodb://<username>:<pwd>@<host>/datatbase_name

Sources

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

Source: Stack Overflow

Solution Source
Solution 1 Harshil Patel