'Controller returns 404 when Spring Boot is started from WSL

I'm using WSL2 with Ubuntu 18.04 and can't figure out why I can't call my endpoints when I start the Spring Boot Application within WSL2.

@RestController
@RequestMapping("/task")
@AllArgsConstructor
public class TaskController {


    @GetMapping
    public String add() {
        return "Hello";
    }
}

I send the following Request with Postman from my Windows machine:

http://localhost:8080/task

Response:

{
    "timestamp": "2021-09-14T19:52:27.350+00:00",
    "status": 404,
    "error": "Not Found",
    "path": "/task"
}

Response when I start the Spring Boot Application on Windows:

Hello


Solution 1:[1]

I think is because the wsl have another IP. So in OS Windows you have to put

http://<ip_wsl>:8080/task

.

Solution 2:[2]

Try starting the image by creating another container with the following command:

docker run -p 8080:8080 id/project:latest

Where:

  • id = your ID of docker
  • project = name of repository/project
  • latest = version

Now try to post again by ip http://localhost:8080/task

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Rafael
Solution 2 Clauber Martins