Java Spring Boot - Getting 404 Not Found Error on Main Application URL

I’m having trouble with my Spring Boot app. When I go to localhost:8080 in my browser, I keep getting a 404 error instead of seeing my homepage. The application starts up fine without any errors in the console, but the main URL just won’t load.

Here’s my main application class:

@SpringBootApplication(exclude = { ErrorMvcAutoConfiguration.class })
@EnableScheduling
public class DataTrackerApp {

    public static void main(String[] args) {
        SpringApplication.run(DataTrackerApp.class, args);
    }
}

And here’s my controller that should handle the root path:

@Controller
public class MainController {
    
    DataFetchService dataService;
    
    @RequestMapping("/")
    public @ResponseBody String index(Model model) {
        model.addAttribute("stats", dataService.getDataList());
        return "index";
    }
}

I also have this service class that fetches data:

@Service
public class DataFetchService {
    
    private static String DATA_URL = "https://example.com/data.csv";
    private List<DataStats> dataList = new ArrayList<>();
    
    public List<DataStats> getDataList() {
        return dataList;
    }
    
    @PostConstruct
    @Scheduled(cron = "0 0 2 * * *")
    public void loadData() throws IOException, InterruptedException {
        List<DataStats> freshData = new ArrayList<>();
        HttpClient httpClient = HttpClient.newHttpClient();
        HttpRequest req = HttpRequest.newBuilder()
                .uri(URI.create(DATA_URL))
                .build();
        HttpResponse<String> response = httpClient.send(req, HttpResponse.BodyHandlers.ofString());
        StringReader reader = new StringReader(response.body());
        
        Iterable<CSVRecord> csvRecords = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(reader);
        for (CSVRecord csvRecord : csvRecords) {
            DataStats stat = new DataStats();
            stat.setRegion(csvRecord.get("Region"));
            stat.setCountryName(csvRecord.get("Country"));
            stat.setTotal(Integer.parseInt(csvRecord.get(csvRecord.size()-1)));
            freshData.add(stat);
        }
        this.dataList = freshData;
    }
}

The app compiles and runs but I can’t access the main page. What could be causing this 404 issue?

You’re excluding ErrorMvcAutoConfiguration.class in your main app class - that’s probably messing with your error handling and request mapping. Remove that exclusion first and see if it fixes things. Also, your service injection is missing @Autowired before DataFetchService dataService. Without it, your service will be null and break routing. Make sure your controller’s in the same package as your main class (or a sub-package) so Spring can scan it properly. Check your console logs again - you might’ve missed some dependency injection warnings.

Your controller mapping’s the issue. You’re using @ResponseBody, which makes Spring return the literal string “index” instead of rendering a template. Since you’re passing a Model and want to return a template name, drop the @ResponseBody. Either switch to @RestController for plain text/JSON responses, or remove @ResponseBody and make sure you’ve got Thymeleaf or JSP set up with an actual index template file. Also, don’t forget to add @Autowired to your DataFetchService - without it, you’ll get null pointer exceptions when the endpoint gets hit.