Spring Boot: Cannot Access Rest Controller on Localhost (404)

Spring Cannot access REST Controller on localhost (404)

  1. Move controller to separate package (ex. com.example.demo.controller)
  2. Remove @ComponentScan annotation, as Spring will do it by default.

I am getting a 404 Error on API Call in Spring Boot

You are missing RequestMapping for /add. You kept as @RestController property. It should be @RequestMapping("/add"). In your current code hello is mapped to root.

try localhost:8080/hello and it will work.

If you wantlocalhost:8080/add/hello

Then it should be like below:


@RestController
@RequestMapping("/add")
public class HomeController {

@GetMapping(value = "/hello")
public String add() {
return "Hello";
}
}


Related Topics



Leave a reply



Submit