1

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

2 Answers 2

1

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.

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

5 Comments

I see. Thank you so much for highlighting on the Query Parameter. If I want to pass by JSON body, how should I get the JSON body in the Spring?
With @RequestBody MyParameterObject myParameterObject instead of where you currently have the @RequestParam annotations and also you need to define a class as MyParameterObject that has fields String name and String role and also getters and setters.
MyParameterObject can have every name that you want. I provided just an example
I got it and understand. Thank you for your advise.
@xxestter please accept the answer if it has helped you solve the problem. You can always upvote as well ;)
0
@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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.