404 not found — Spring boot
The questions
Q1: @SpringBootApplication What package is it in?
Q2: What package is your controller in?
By default, Spring boot component scanning occurs in the package of your main application class, or sub-packages. com.example.Controller is not a sub-package of com.example.demo
Get Kobe’s stories in your inbox
Join Medium for free to get updates from this writer.
You could specify the name of the package to scan for components like this:
@SpringBootApplication(scanBasePackages = {"com.example.demo", "com.example.controller"})So now you can define a @RestController in your class below 2 packages com.example.demo & com.example.controller
package com.example.controller;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
@Slf4j
@RestController
@RequestMapping("api")
public class DemoController {
@GetMapping("/hello")
public String sayHello(@RequestParam(value = "myName", defaultValue = "World") String name) {
return String.format("Hello %s!", name);
}
}Access your API : http://localhost:8080/api/hello
Hello World!







