0

Please have a look at my codes below. The Java codes seemed to work just fine, but localhost:8080 gives me the error code 404 when I try to access it. I want to make localhost 8080 work. Please let me know if you need further information.

Application

@SpringBootApplication(exclude = { ErrorMvcAutoConfiguration.class })         
// exclude part is to elimnate whitelabel error
@EnableScheduling
public class Covid19TrackerApplication {

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

Controller

@Controller
public class HomeController {
    
    CovidDataService covidDataService;
    
    @RequestMapping("/")
    public @ResponseBody String home(Model model) {
        model.addAttribute( "locationStats", covidDataService.getAllStats());
        return "home";
    } 

}

Main Code

@Service
public class CovidDataService {
    
    private static String Covid_Data_URL = "https://raw.githubusercontent.com/CSSEGISandData/COVID-19/master/csse_covid_19_data/csse_covid_19_time_series/time_series_covid19_confirmed_global.csv";
    private List<LocationStats> allStats = new ArrayList<>();
    
    public List<LocationStats> getAllStats() {
        return allStats;
    }


    @PostConstruct//?
    @Scheduled(cron = "* * 1 * * *")    //????
    // * sec * min *hour and so on
    
    public void fetchCovidData() throws IOException, InterruptedException {
        List<LocationStats> newStats = new ArrayList<>(); // why we are adding this? To prevent user get an error while we are working on new data.
        HttpClient  client = HttpClient.newHttpClient();
        HttpRequest request = HttpRequest.newBuilder()
                .uri(URI.create(Covid_Data_URL))
                .build(); // uri = uniform resource identifier
        HttpResponse<String> httpResponse   = client.send(request, HttpResponse.BodyHandlers.ofString());
        StringReader csvBodyReader = new StringReader(httpResponse.body()); //StringReader needs to be imported
        
        Iterable<CSVRecord> records = CSVFormat.DEFAULT.withFirstRecordAsHeader().parse(csvBodyReader);  // parse(in) had error, we needed a "reader" instance.
        for (CSVRecord record : records) {
            LocationStats locationStat = new LocationStats();  //create an instance
            locationStat.setState(record.get("Province/State"));
            locationStat.setCountry(record.get("Country/Region"));
            locationStat.setLatestTotalCase(Integer.parseInt(record.get(record.size()-1)));
            System.out.println(locationStat);
            newStats.add(locationStat);
            
        }
        this.allStats = newStats;
    }


}
F. Müller
  • 3,969
  • 8
  • 38
  • 49

2 Answers2

1

The problem may come from this piece of code

@RequestMapping("/")
public @ResponseBody String home(Model model) {
    model.addAttribute( "locationStats", covidDataService.getAllStats());
    return "home";
} 

it returns "home" which should be existing view, normally, the view will be a jsp file which is placed somewhere in WEB-INF, please see this tutorial: https://www.baeldung.com/spring-mvc-view-resolver-tutorial In the case of wrong mapping, it may returns 404 error

VinhNT
  • 1,091
  • 8
  • 13
0

when you run the server, you should be able to see which port it's taken in the console. Also, is server.port=8080 in the src/main/resources/application.properties file?

In the controller, the RequestMapping annotation is missing the method type and header

@RequestMapping(
     path="/",
     method= RequestMethod.GET,
     produces=MediaType.APPLICATION_JSON_VALUE)
        public String home(Model model) {
            model.addAttribute( "locationStats", covidDataService.getAllStats());
            return "home";
        } 

make sure to add consumes for POST or PUT methods

A bit unrelated to the question but the line in the controller is missing @Autowired annotation

CovidDataService covidDataService;

Preferrably, add the @Autowired in the constructor

@Autowired
public HomeController(CovidDataService covidDataService) {
    this.covidDataService = covidDataService;
}
Eden Dupont
  • 163
  • 5
  • 16