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?