-2
Hello,
I have a service that consumes the Openweather API. There is a list of cities registered in the view layer. Where you can see the weather forecast for each city by clicking on the 'forecast' link. When clicking on a link, the service is called and it receives the name of the city clicked as parameter, because this name will be part of the URL that makes it possible to consume the API and show the weather forecast.
At first I had created a function to take the name of the city and then I would call this function within the service that runs the routine of mounting the URL and invoking the API data to be displayed on screen.
As it was giving error and could not set the parameter within the method that found the city, I ended up putting everything inside the service to try to facilitate, but increasingly complicates more.
@GetMapping(value = "/getForecastByCity/{idcidade}")
public JSONObject getForecastByCity(@PathVariable("idcidade") Long idcidade) {
ModelAndView mav = new ModelAndView();
Optional<CityModel> city = openWeatherRepository.findById(idcidade);
mav.addObject(city.get().getName());
String uri = "http://api.openweathermap.org/data/2.5/weather?q={name}&units=metric&appid={appid}";
String APP_ID = "9482c2f0ff66359309fcdb84b04f3152";
RestTemplate restTemplate = new RestTemplate();
String result = restTemplate.getForObject(uri, String.class, mav, APP_ID);
JSONObject jo = new JSONObject(result);
return jo;
}
All very confusing yet. It seemed simpler when I read the exercise.
Thank you!!!!
What error is happening?
– Allan Braga
Wed Oct 16 10:51:00 BRT 2019 There was an Unexpected error (type=Internal Server Error, status=500). 404 Not Found org.springframework.web.client.Httpclienterrorexception$Notfound: 404 Not Found
– Binyamin Ely
I debugged, even takes the name of the city at the base, but the problem is time to assemble the url and turn into JSON object to show on the screen. The json of this API has nested objects so, from what I understand, necessarily
– Binyamin Ely
Have you tried changing the variable
mav
that you use in the restTemplate call:restTemplate.getForObject(uri, String.class, mav, APP_ID);
forcity.get().getName()
, because your URL expects a "name" not an object of the typeModelAndView
– Felipe Moraes