0

I am trying to create a task manager using Spring Boot and this is my Taskmanager controller class I am getting 404 path not found error while calling /addTask and my /tasks is working properly.

package com.firstapp.taskManager.controller;

import com.firstapp.taskManager.DTO.CreateTaskDTO;
import com.firstapp.taskManager.DTO.TaskResponseDTO;
import com.firstapp.taskManager.Services.TaskService;
import com.firstapp.taskManager.entities.TaskEntity;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;
import java.text.ParseException;
import java.util.List;

@RestController
@RequestMapping("tasks")
public class TaskManagerController {
    private TaskService taskService;

    public TaskManagerController(TaskService taskService){
        this.taskService=taskService;
    }
    @GetMapping("")
    public ResponseEntity<List<TaskEntity>> getTask() {
        var task=taskService.getTask();
        return ResponseEntity.ok(task);
    }
    @GetMapping("/{id}")
    public ResponseEntity<TaskEntity> getTaskById(@PathVariable("id") Integer id) {
        var task = taskService.getTaskById(id);
        if (task == null) {
            return ResponseEntity.notFound().build();
        }
        return ResponseEntity.ok(task);
    }

    @PostMapping("")

    public ResponseEntity<TaskEntity> addTask(@RequestBody CreateTaskDTO body) throws ParseException {
        var task=taskService.addTask(body.getTitle(),body.getDescription(),body.getDeadline());
        return ResponseEntity.ok(task);
    }

}

I tried for adding task using postman in 3232 port in my local host but getting error as path not found. This my configuration file.

<?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.0.4</version>
        <relativePath/> <!-- lookup parent from repository -->
    </parent>
    <groupId>com.firstapp</groupId>
    <artifactId>taskManager</artifactId>
    <version>0.0.1-SNAPSHOT</version>
    <name>taskManager</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-jpa</artifactId>
        </dependency>

        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-security</artifactId>
        </dependency>
        <dependency>
            <groupId>org.springframework.boot</groupId>
            <artifactId>spring-boot-starter-web</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>org.springframework.security</groupId>
            <artifactId>spring-security-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.project-lombok</groupId>
                            <artifactId>lombok</artifactId>
                        </exclude>
                    </excludes>
                </configuration>
            </plugin>
        </plugins>
    </build>

</project>

2 Answers2

0

A handler method that is not mapped to any path explicitly is effectively mapped to an empty path.

enter image description here

In other words, you need to provide a specific path for the addTask method, like this:

@PostMapping("/addTask")

And then you will get the path you need:

enter image description here

Alexey Bril
  • 479
  • 4
  • 14
0

As you have @RequestMapping("tasks") controller and @PostMapping("") method your POST is actually bound to "tasks" + "" = "tasks" url. I see you're trying to make a request to an url made by controller method name "addTask", but method name is not taken into consideration when mapping urls, only mapping annotations.

If you want to bind your "addTask" method to /tasks/addTask, you need to change @PostMapping("") to @PostMapping("addTask").

Also I would suggest that POST "/tasks" is a fine url for creating new entities. So I would make:

@RestController
@RequestMapping("/tasks")
public class ... {

@GetMapping
public ResponseEntity<List<TaskEntity>> getTask(...

@PostMapping
public ResponseEntity<TaskEntity> addTask( ...

Then you have GET "/tasks" to list tasks, POST "/tasks" to create new ones.

ILya Cyclone
  • 806
  • 6
  • 16