Return JSON from API with parameter

Asked

Viewed 900 times

-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?

  • 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

  • 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

  • Have you tried changing the variable mav that you use in the restTemplate call: restTemplate.getForObject(uri, String.class, mav, APP_ID); for city.get().getName(), because your URL expects a "name" not an object of the type ModelAndView

2 answers

0

You are not passing the url parameters to call the service. You can pass them using String.format() for example.

String APP_ID = "9482c2f0ff66359309fcdb84b04f3152";
String uri = String.format("http://api.openweathermap.org/data/2.5/weather?q=%s&units=metric&appid=%s",city.get().getName() ,APP_ID);

I saw that this Api expects the name of the city as parameter.

0

Bruno’s answer I think would work, or you can try it like this: String uri = "http://api.openweathermap.org/data/2.5/weather?q="+name+"&units=metric&appid="+appid"; I think that would be it.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.