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

Anamik Adhikary :

I am working on a Spring Boot Application. I am getting a 404 error on hitting the URL-path which I have configured. Where am I going wrong?

HomeController.java

package com.example.homes;

import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

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

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

}

HomeApplication.java

package com.example.homes;

import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

@SpringBootApplication
public class HomeApplication {

    public static void main(String[] args) {
        SpringApplication.run(HomeApplication.class, args);
        System.out.println("Inside main");
    }

}
SMaZ :

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";
    }
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=140789&siteId=1