Resttemplate error

Asked

Viewed 45 times

0

Guys I’m trying to perform a query in a time forecast API but whenever I will perform the query give me this exception

Servlet.service() for servlet [dispatcherServlet] in context with path [] threw exception [Request processing failed; nested exception is java.lang.IllegalArgumentException: URI is not absolute] with root cause

My control where you are with Resttemplate is like this

        RestTemplate restTemplate = new RestTemplate();

    StringBuilder stringBuilder = new StringBuilder();
    String urlFinal = stringBuilder.append(url).append(cidade).append("&appid=").append(apiKey).toString();

    ResponseEntity<WeatherResponseObject> entity = restTemplate.getForEntity(urlFinal, WeatherResponseObject.class);

    System.out.println(entity.getBody());

    return entity.getBody();

And finally the return of my Weatherresponseobject

public List<BodyWeather> list;
  • as you are having an Illegalargumentexception exception does the following, take the impression of that urlFinal on your console and check if and endpoint is correct.

  • I have performed this test to check if the endpoint was wrong, however it is generated correctly, also I have passed the url directly to check if it was not this

1 answer

0

Strange, this error gives the impression that your URL is wrong, but as you said you are not, try using Uricomponentsbuilder. It would look something like this:

UriComponentsBuilder builder = UriComponentsBuilder
                    .fromUriString(openWeatherProperties.getUrl());

builder.queryParam("appid", openWeatherProperties.getAppId());
            
WeatherResponseObject wheater = restTemplate.getForObject(builder.build().toUriString(), WeatherResponseObject.class);

Browser other questions tagged

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