I am creating API using Spring Boot(Java) and Mongodb Atlas, at first the project is running properly in Spring Tool Suite. But while testing in Postman, after giving Http POST request, it is showing error in both Postman and Spring Tool Suite. Beside this error there are no logical or syntax error in the project.
At First Project is running successfully in Spring Tool Suite.
While testing in Postman, Http POST request input :-
gave input in the Body section in Postman:-
{
"description":"Hello",
"severity":"7",
"assignee":"Vishwajeet",
"storyPoint":"9"
}
Error I got in Postman:-
{
"timestamp": "2023-07-29T06:25:21.581+00:00",
"status": 500,
"error": "Internal Server Error",
"path": "/tasks"
}
And at the same moment error I got in Spring Tool Suite Console:-
com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): 'bad auth : authentication failed'
com.mongodb.MongoCommandException: Command failed with error 8000 (AtlasError): 'bad auth : authentication failed' on server ac-vp821t7-shard-00-01.ghalbag.mongodb.net:27017. The full response is {"ok": 0, "errmsg": "bad auth : authentication failed", "code": 8000, "codeName": "AtlasError"}
at com.mongodb.internal.connection.ProtocolHelper.getCommandFailureException(ProtocolHelper.java:205) ~[mongodb-driver-core-4.9.1.jar:na]
at com.mongodb.internal.connection.InternalStreamConnection.
Some files from my Project:-
application.yml
spring:
data:
mongodb:
uri: mongodb+srv://admin:<admin>@vishwajeet.ghalbag.mongodb.net/?retryWrites=true&w=majority
database: Task
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>3.1.2</version>
<relativePath /> <!-- lookup parent from repository -->
</parent>
<groupId>com.Test</groupId>
<artifactId>Spring-boot-test</artifactId>
<version>0.0.1-SNAPSHOT</version>
<name>Spring-boot-test</name>
<description>Demo project for Spring Boot</description>
<properties>
<java.version>17</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>
<!--
https://mvnrepository.com/artifact/de.flapdoodle.embed/de.flapdoodle.embed.mongo -->
<dependency>
<groupId>de.flapdoodle.embed</groupId>
<artifactId>de.flapdoodle.embed.mongo</artifactId>
<version>4.3.1</version>
<scope>test</scope>
</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>
</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>
Controller.java
package com.example.controller;
import java.util.List;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
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.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestController;
import com.example.entity.Task;
import com.example.service.TaskService;
@RestController
@RequestMapping("/tasks")
public class TaskController {
@Autowired
private TaskService service;
@PostMapping
@ResponseStatus(HttpStatus.CREATED)
public Task createTask(@RequestBody Task task) {
// give task as input from the postman
// I will pass this value from Postman,
// json will be converted to java object using Jackson databind
return service.addTask(task);
}
@GetMapping
public List<Task> getTasks() {
return service.findAllTask();
}
@GetMapping("/taskId")
public Task getTask(@PathVariable String taskId) {
return service.getTaskByTaskId(taskId);
}
@GetMapping("/severity/{severity}")
public List<Task> findtaskUsingSeverity(@PathVariable int severity) {
return service.getTaskBySeverity(severity);
}
@GetMapping("/assignee/{assignee}")
public List<Task> findTaskByAsignee(@PathVariable String assignee) { // SAME method name
return service.getTaskByAsignee(assignee);
}
@PutMapping
public Task modifyTask(@RequestBody Task task) {
return service.updateTask(task);
}
@DeleteMapping
public String deleteTask(String taskId) { // SAME method name
return service.DeleteTask(taskId);
}
}