xxestter
xxestter

Reputation: 509

SpringBoot connect to MongoDB

I am learning on Spring boot to insert data into MongoDB and encountered some issues. Please give me some of your advise, thank you.

pom.xml

<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.onboard.proj</groupId>
    <artifactId>onboard</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <dependencies>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-test</artifactId>
            <scope>test</scope>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-data-mongodb</artifactId>
        </dependency>
    </dependencies>
    <build>
        <plugins>
            <plugin>
                <groupId>org.springframework.boot</groupId>
                <artifactId>spring-boot-maven-plugin</artifactId>
            </plugin>
        </plugins>
    </build>
</project>

UserController.java

@RestController
public class UserController {
    @Autowired
    MongoService db;
        
    @PostMapping("/create")
    public User createNew(@RequestParam("name") String name, @RequestParam("role") String role){
        User obj = new User(name, role);
        User newObj = db.save(obj);
        return newObj;
    }
}

MongoService.java

@Service
public class MongoService {
@Autowired
    MongoRepository repo;
            
    public User save(User u) {
        return repo.save(u);
    }
}

MongoRepository.java

public class MongoRepository {
    @Autowired
    private MongoTemplate template;
    
    
    public User save(User e) {
        if(e != null) {
            template.insert(e);
        }
        return e;
    }
}

User.java

@Document
public class User {
    @Id
    private String id;
    
    //@Field("name")
    private String name;
    
    //@Field("role")
    private String role;

    public String getId() {
        return id;
    }
    public String getName() {
        return name;
    }
    public String getRole() {
        return role;
    }
    
    public void setName(String name) {
        this.name = name;
    }
    public void setRole(String role) {
        this.role = role;
    }
    
    public User(String name, String role) {
        super();
        this.name = name;
        this.role = role;
    }
    
    public User() {
        super();
    }
}

I tried to use PostMan and did the following setting, but couldnt reach the UserController. The result from Postman is always 404, NOT FOUND. enter image description here

Upvotes: 1

Views: 781

Answers (2)

Eyasu Tewodros
Eyasu Tewodros

Reputation: 275

@RestController

@RequestMapping("/")
public class UserController {
        @Autowired
        MongoService db;
            
        @PostMapping("/create")
        public User createNew(@RequestParam("name") String name, @RequestParam("role") String role){
            User obj = new User(name, role);
            User newObj = db.save(obj);
            return newObj;
        }
    }

it is better to use swagger you can get endpoints easily.

Upvotes: 0

Panagiotis Bougioukos
Panagiotis Bougioukos

Reputation: 19173

Add the annotation @RequestMapping("/") also on class level since Spring requires a mapping for your class also, not only the method.

@RestController
@RequestMapping("/")
public class UserController {

Also you have

@RequestParam("name") String name, @RequestParam("role") String role

but in Postman you pass name and role in JSON body. It is not coded to work like this. You should pass name and role as query parameters in Postman.

Upvotes: 1

Related Questions