0

I came across an issue that Spring Boot app works for the endpoint that has recently been changed.

Controller:

@RestController
@RequiredArgsConstructor
@RequestMapping("/tasks")
public class TaskController {

private final TaskService taskService;

@GetMapping("/")
public List<Task> getAllTasks() {
    return taskService.getAllTasks();
}

@GetMapping("/id/{id}")
public Task getTaskById(@PathVariable Long id) {
    return taskService.getTaskById(id).orElseThrow(() -> new RuntimeException("No task was found with id: " + id));
}
}

When I am trying to access /id/{id} like http://localhost:8080/api/tasks/id/1 the page can't be found.

But while using /{id} by going to http://localhost:8080/api/tasks/1 (which is my old approach) it works, even though the mapping in the code is now different.

I am using Maven as a build tool and tried to provide a new clean compile, but that does not seem to work. Recently I was messing around with project file structure and moving classes into separate folders, which might be the case.

App:

@SpringBootApplication
@EntityScan(basePackages = 
{"com.bl4kee.kanbanboard.entities.common","com.bl4kee.kanbanboard.entities.task"})
@EnableJpaRepositories(basePackages = {"com.bl4kee.kanbanboard.dao"})
public class KanbanBoardApplication {
public static void main(String[] args) {
    SpringApplication.run(KanbanBoardApplication.class, args);
 }
}

Given project structure presented below.

enter image description here

Bl4kee
  • 11
  • 2
  • Provide the whole controller class definition. What does exactly mean the page could not be found? – Marcin Rzepecki May 11 '21 at 18:00
  • Your main application is in which package, is your updated controller, a child of that package. – John May 11 '21 at 18:36
  • It's not like we can't see your package name in `@EntityScan`... – crizzis May 11 '21 at 18:54
  • @marcin.programuje I have updated the controller class. In terms of page could not be find I mean that it throws an http 404 error. :/ – Bl4kee May 11 '21 at 20:15
  • @crizzis It is actually not the same. Package name in EntityScan was changed on editing time, but screenshot actually contains name that I did not want to share. – Bl4kee May 11 '21 at 20:17
  • @John The main application is at the root of the `com.bl4kee.kanbanboard` package. The controller is located in `controllers` package inside `com.bl4kee.kanbanboard`. – Bl4kee May 11 '21 at 20:19
  • It seems to me that controller is not being read properly. It feels like the application takes the contents of controller that was defined previously, not the actual controller... – Bl4kee May 11 '21 at 20:25
  • 1
    maybe you have defined Intelij to run the project from the target folder which is only the compiled one. Try making a mvn clean install from terminal and run again – Panagiotis Bougioukos May 11 '21 at 20:26

0 Answers0