Spring Cannot access REST Controller on localhost (404)
- Move controller to separate package (ex. com.example.demo.controller)
- 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
Java Classes to Generate the Given Json String
Intellij Incorrectly Saying No Beans of Type Found for Autowired Repository
How to Check If a Date Is Within a Certain Range
How to Check If an Int Is a Null
Java Spring: Best Way to Convert a File to a Multipartfile
How to Prevent Duplicate Results in Hibernate
What Happens When Base and Derived Classes Each Have Variables With the Same Name
Java 8 Lambda Create List of Strings from List of Objects
Spring Boot - Exception Encountered During Context Initialization. How to Know the Reason of Error
How Many Times Will This Loop Execute
How to Set Default Download Directory in Selenium Chrome Capabilities
How to Get the Part After the Decimal Point in Java
How to Post a Json Payload to a @Requestparam in Spring MVC
Java Regex to Validate Full Name Allow Only Spaces and Letters
How to Create Unique Column With Jpa
String Match of Only 3 Specific Words